0

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.

4
  • 1
    looks at clock Yup, it was about time for a terrible idea to come around... Oct 22, 2010 at 6:23
  • Terrible ideas are good ideas sometimes =D
    – Falmarri
    Oct 22, 2010 at 6:26
  • could you elaborate why you are trying to do this and what the desired end result is? Oct 22, 2010 at 6:51
  • Well mostly because I like needlessly complicated regexes. But I was just curous mainly
    – Falmarri
    Oct 22, 2010 at 6:54

3 Answers 3

1

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.

1

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.