diff options
author | Fredrik Lundh <fredrik@pythonware.com> | 2000-07-02 22:25:39 (GMT) |
---|---|---|
committer | Fredrik Lundh <fredrik@pythonware.com> | 2000-07-02 22:25:39 (GMT) |
commit | c2301730b8e07ece0b7c4ff6b6b06a4895d370c7 (patch) | |
tree | fa5dd23128e9b5acc45dfed3958c341bdb90be73 | |
parent | c9c02c4cf3d3e508986d1e7a6840f3ef92f077a6 (diff) | |
download | cpython-c2301730b8e07ece0b7c4ff6b6b06a4895d370c7.zip cpython-c2301730b8e07ece0b7c4ff6b6b06a4895d370c7.tar.gz cpython-c2301730b8e07ece0b7c4ff6b6b06a4895d370c7.tar.bz2 |
- experimental: added two new attributes to the match object:
"lastgroup" is the name of the last matched capturing group,
"lastindex" is the index of the same group. if no group was
matched, both attributes are set to None.
the (?P#) feature will be removed in the next relase.
-rw-r--r-- | Lib/sre_compile.py | 8 | ||||
-rw-r--r-- | Lib/sre_parse.py | 2 | ||||
-rw-r--r-- | Modules/_sre.c | 40 | ||||
-rw-r--r-- | Modules/sre.h | 1 |
4 files changed, 38 insertions, 13 deletions
diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py index e5c501e..36986eb 100644 --- a/Lib/sre_compile.py +++ b/Lib/sre_compile.py @@ -298,8 +298,14 @@ def compile(p, flags=0): assert p.pattern.groups <= 100,\ "sorry, but this version only supports 100 named groups" + # map in either direction + groupindex = p.pattern.groupdict + indexgroup = [None] * p.pattern.groups + for k, i in groupindex.items(): + indexgroup[i] = k + return _sre.compile( pattern, flags, array.array(WORDSIZE, code).tostring(), - p.pattern.groups-1, p.pattern.groupdict + p.pattern.groups-1, groupindex, indexgroup ) diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 81ca217..d78737f 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -515,6 +515,8 @@ def _parse(source, state): group = state.getgroup(name) while 1: p = _parse(source, state) + if group is not None: + p.append((INDEX, group)) if source.match(")"): if b: b.append(p) diff --git a/Modules/_sre.c b/Modules/_sre.c index e11a892..d6f050e 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -1104,9 +1104,10 @@ _compile(PyObject* self_, PyObject* args) PyObject* code; int groups = 0; PyObject* groupindex = NULL; - if (!PyArg_ParseTuple(args, "OiO!|iO", &pattern, &flags, + PyObject* indexgroup = NULL; + if (!PyArg_ParseTuple(args, "OiO!|iOO", &pattern, &flags, &PyString_Type, &code, - &groups, &groupindex)) + &groups, &groupindex, &indexgroup)) return NULL; self = PyObject_NEW(PatternObject, &Pattern_Type); @@ -1127,6 +1128,9 @@ _compile(PyObject* self_, PyObject* args) Py_XINCREF(groupindex); self->groupindex = groupindex; + Py_XINCREF(indexgroup); + self->indexgroup = indexgroup; + return (PyObject*) self; } @@ -1883,7 +1887,28 @@ match_getattr(MatchObject* self, char* name) PyErr_Clear(); - /* attributes */ + if (!strcmp(name, "lastindex")) { + /* experimental */ + if (self->index >= 0) + return Py_BuildValue("i", self->index); + Py_INCREF(Py_None); + return Py_None; + } + + if (!strcmp(name, "lastgroup")) { + /* experimental */ + if (self->pattern->indexgroup) { + PyObject* result = PySequence_GetItem( + self->pattern->indexgroup, self->index + ); + if (result) + return result; + PyErr_Clear(); + } + Py_INCREF(Py_None); + return Py_None; + } + if (!strcmp(name, "string")) { Py_INCREF(self->string); return self->string; @@ -1900,15 +1925,6 @@ match_getattr(MatchObject* self, char* name) if (!strcmp(name, "endpos")) return Py_BuildValue("i", 0); /* FIXME */ - if (!strcmp(name, "index")) { - /* experimental */ - if (self->index < 0) { - Py_INCREF(Py_None); - return Py_None; - } else - return Py_BuildValue("i", self->index); - } - PyErr_SetString(PyExc_AttributeError, name); return NULL; } diff --git a/Modules/sre.h b/Modules/sre.h index 7e7d835..f66d608 100644 --- a/Modules/sre.h +++ b/Modules/sre.h @@ -21,6 +21,7 @@ typedef struct { PyObject* code; /* link to the code string object */ int groups; PyObject* groupindex; + PyObject* indexgroup; /* compatibility */ PyObject* pattern; /* pattern source (or None) */ int flags; /* flags used when compiling pattern source */ |