mirror of
https://github.com/python/cpython
synced 2024-10-17 00:23:23 +00:00
(experimental) "finditer" method/function. this works pretty much
like findall, but returns an iterator (which returns match objects) instead of a list of strings/tuples.
This commit is contained in:
parent
9242a4af17
commit
703ce8122c
10
Lib/sre.py
10
Lib/sre.py
|
@ -93,6 +93,7 @@
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
import sre_compile
|
import sre_compile
|
||||||
import sre_parse
|
import sre_parse
|
||||||
|
|
||||||
|
@ -164,6 +165,15 @@ def findall(pattern, string):
|
||||||
Empty matches are included in the result."""
|
Empty matches are included in the result."""
|
||||||
return _compile(pattern, 0).findall(string)
|
return _compile(pattern, 0).findall(string)
|
||||||
|
|
||||||
|
if sys.hexversion >= 0x02020000:
|
||||||
|
def finditer(pattern, string):
|
||||||
|
"""Return an iterator over all non-overlapping matches in
|
||||||
|
the string. For each match, the iterator returns a match
|
||||||
|
object.
|
||||||
|
|
||||||
|
Empty matches are included in the result."""
|
||||||
|
return _compile(pattern, 0).finditer(string)
|
||||||
|
|
||||||
def compile(pattern, flags=0):
|
def compile(pattern, flags=0):
|
||||||
"Compile a regular expression pattern, returning a pattern object."
|
"Compile a regular expression pattern, returning a pattern object."
|
||||||
return _compile(pattern, flags)
|
return _compile(pattern, flags)
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
* 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1
|
* 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1
|
||||||
* 2001-10-21 fl added sub/subn primitive
|
* 2001-10-21 fl added sub/subn primitive
|
||||||
* 2001-10-22 fl check for literal sub/subn templates
|
* 2001-10-22 fl check for literal sub/subn templates
|
||||||
|
* 2001-10-24 fl added finditer primitive (for 2.2 only)
|
||||||
*
|
*
|
||||||
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
|
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
|
||||||
*
|
*
|
||||||
|
@ -1954,6 +1955,30 @@ pattern_findall(PatternObject* self, PyObject* args, PyObject* kw)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if PY_VERSION_HEX >= 0x02020000
|
||||||
|
static PyObject*
|
||||||
|
pattern_finditer(PatternObject* pattern, PyObject* args)
|
||||||
|
{
|
||||||
|
PyObject* scanner;
|
||||||
|
PyObject* search;
|
||||||
|
PyObject* iterator;
|
||||||
|
|
||||||
|
scanner = pattern_scanner(pattern, args);
|
||||||
|
if (!scanner)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
search = PyObject_GetAttrString(scanner, "search");
|
||||||
|
Py_DECREF(scanner);
|
||||||
|
if (!search)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
iterator = PyCallIter_New(search, Py_None);
|
||||||
|
Py_DECREF(search);
|
||||||
|
|
||||||
|
return iterator;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
|
pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
|
||||||
{
|
{
|
||||||
|
@ -2331,6 +2356,9 @@ static PyMethodDef pattern_methods[] = {
|
||||||
{"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS},
|
{"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS},
|
||||||
{"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS},
|
{"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS},
|
||||||
{"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS},
|
{"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS},
|
||||||
|
#if PY_VERSION_HEX >= 0x02020000
|
||||||
|
{"finditer", (PyCFunction) pattern_finditer, METH_VARARGS},
|
||||||
|
#endif
|
||||||
{"scanner", (PyCFunction) pattern_scanner, METH_VARARGS},
|
{"scanner", (PyCFunction) pattern_scanner, METH_VARARGS},
|
||||||
{"__copy__", (PyCFunction) pattern_copy, METH_VARARGS},
|
{"__copy__", (PyCFunction) pattern_copy, METH_VARARGS},
|
||||||
{"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_VARARGS},
|
{"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_VARARGS},
|
||||||
|
|
Loading…
Reference in a new issue