views:

35

answers:

3

Can you have dynamic naming in regex groups? Something like

reg = re.compile(r"(?PText|Or|Something).*(?PTextIWant)")

r = reg.find("TextintermingledwithTextIWant")

r.groupdict()["Text"] == "TextIWant"

So that depending on what the beggining was, group["Text"] == TextIWant

Updated to make the quesetion more clear.

A: 

You could first build the string in a dynamic way and then pass it to the Regex engine.

npinti
+1  A: 

Your question is worded kind of funny, but I think what you are looking for is a non-capturing group. Make it like this:

(?:Must_Match_This_First)What_You_Want(?:Must_Match_This_Last)

The ?: is what designates a that a group matches, but does not capture.

typoknig
+1  A: 

Some regex engines support this, some don't. This site says that Perl, Python, PCRE (and thus PHP), and .NET support it, all with slightly different syntax:

+--------+----------------------------+----------------------+------------------+
| Engine |           Syntax           |    Backreference     |     Variable     |
+--------+----------------------------+----------------------+------------------+
| Perl   | (?<name>...), (?'name'...) | \k<name>, \k'name'   | %+{name}         |
|        | (?P<name>...)              | \g{name}, (?&name)*  |                  |
|        |                            | (?P>name)*           |                  |
+--------+----------------------------+----------------------+------------------+
| Python | (?P<name>...)              | (?P=name), \g<name>  | m.group('name')  |
+--------+----------------------------+----------------------+------------------+
| .NET   | (?<name>...), (?'name'...) | \k<name>, \k'name'   | m.Groups['name'] |
+--------+----------------------------+----------------------+------------------+
| PCRE   | (?<name>...), (?'name'...) | \k<name>, \k'name'   | Depends on host  |
|        | (?P<name>...)              | \g{name}, \g<name>*  | language.        |
|        |                            | \g'name'*, (?&name)* |                  |
|        |                            | (?P>name)*           |                  |
+--------+----------------------------+----------------------+------------------+

This is not a complete list, but it's what I could find. If you know more flavors, add them! The backreference forms with a * are those which are "recursive" as opposed to just a back-reference; I believe this means they match the pattern again, not what was matched by the pattern. Also, I arrived at this by reading the docs, but there could well be errors—this includes some languages I've never used and some features I've never used. Let me know if something's wrong.

Antal S-Z