Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 703ce81

Browse files
author
Fredrik Lundh
committed
(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.
1 parent 9242a4a commit 703ce81

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lib/sre.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
9494
"""
9595

96+
import sys
9697
import sre_compile
9798
import sre_parse
9899

@@ -164,6 +165,15 @@ def findall(pattern, string):
164165
Empty matches are included in the result."""
165166
return _compile(pattern, 0).findall(string)
166167

168+
if sys.hexversion >= 0x02020000:
169+
def finditer(pattern, string):
170+
"""Return an iterator over all non-overlapping matches in
171+
the string. For each match, the iterator returns a match
172+
object.
173+
174+
Empty matches are included in the result."""
175+
return _compile(pattern, 0).finditer(string)
176+
167177
def compile(pattern, flags=0):
168178
"Compile a regular expression pattern, returning a pattern object."
169179
return _compile(pattern, flags)

Modules/_sre.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1
3636
* 2001-10-21 fl added sub/subn primitive
3737
* 2001-10-22 fl check for literal sub/subn templates
38+
* 2001-10-24 fl added finditer primitive (for 2.2 only)
3839
*
3940
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
4041
*
@@ -1954,6 +1955,30 @@ pattern_findall(PatternObject* self, PyObject* args, PyObject* kw)
19541955

19551956
}
19561957

1958+
#if PY_VERSION_HEX >= 0x02020000
1959+
static PyObject*
1960+
pattern_finditer(PatternObject* pattern, PyObject* args)
1961+
{
1962+
PyObject* scanner;
1963+
PyObject* search;
1964+
PyObject* iterator;
1965+
1966+
scanner = pattern_scanner(pattern, args);
1967+
if (!scanner)
1968+
return NULL;
1969+
1970+
search = PyObject_GetAttrString(scanner, "search");
1971+
Py_DECREF(scanner);
1972+
if (!search)
1973+
return NULL;
1974+
1975+
iterator = PyCallIter_New(search, Py_None);
1976+
Py_DECREF(search);
1977+
1978+
return iterator;
1979+
}
1980+
#endif
1981+
19571982
static PyObject*
19581983
pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
19591984
{
@@ -2331,6 +2356,9 @@ static PyMethodDef pattern_methods[] = {
23312356
{"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS},
23322357
{"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS},
23332358
{"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS},
2359+
#if PY_VERSION_HEX >= 0x02020000
2360+
{"finditer", (PyCFunction) pattern_finditer, METH_VARARGS},
2361+
#endif
23342362
{"scanner", (PyCFunction) pattern_scanner, METH_VARARGS},
23352363
{"__copy__", (PyCFunction) pattern_copy, METH_VARARGS},
23362364
{"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_VARARGS},

0 commit comments

Comments
 (0)