]> granicus.if.org Git - python/commitdiff
- experimental: added two new attributes to the match object:
authorFredrik Lundh <fredrik@pythonware.com>
Sun, 2 Jul 2000 22:25:39 +0000 (22:25 +0000)
committerFredrik Lundh <fredrik@pythonware.com>
Sun, 2 Jul 2000 22:25:39 +0000 (22:25 +0000)
  "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.

Lib/sre_compile.py
Lib/sre_parse.py
Modules/_sre.c
Modules/sre.h

index e5c501edd111eca6c3a69fd486fe88ad7b6ef036..36986eb3e5da08a6bda75529d4bf7e2e264ef0da 100644 (file)
@@ -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
         )
index 81ca217a470fbc3a839610a6e266636fab85d992..d78737f2a58aea262f4d81ae5b74a8cd60cac522 100644 (file)
@@ -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)
index e11a8923dca3e00a34e24fd59bfea0afe7ed3dd3..d6f050ec75a70fd208eeb20ef0595a61bf579336 100644 (file)
@@ -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;
 }
index 7e7d8356e53dc65a5416232a37ce0cb559911a15..f66d608bf6b8ff4b21428e58523ecc810bec7787 100644 (file)
@@ -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 */