1

in short : I want to use the Lookahead technique in Python with the ?P<name> convention (details here) to get the groups by name.

more details :

I discovered the Lookahead trick here; e.g. the following regex...

/^(?=.*Tim)(?=.*stupid).+

... allows to detect strings like "Tim stupid" or "stupid Tim", the order being not important.

I can't figure out how I can combine the ?= "operator" with the ?P one; the following regex obviously doesn't do the trick but gives an idea of what I want :

/^(?=?P<word1>.*Tim)(?=?P<word2>.*stupid).+
Community
  • 1
  • 1
suizokukan
  • 1,303
  • 4
  • 18
  • 33

1 Answers1

1

The ?P<word1> in your regex reminds of a named capture group:

The syntax for a named group is one of the Python-specific extensions: (?P<name>...). *name* is, obviously, the name of the group. Named groups also behave exactly like capturing groups, and additionally associate a name with a group.

So, most probably you are looking for a way to capture substrings inside a positive lookahead anchored at the start to require a string to meet both patterns, and capture the substrings inside both the lookaheads:

^(?=(?P<word1>.*Tim))(?=(?P<word2>.*stupid)).+
    ^^^^^^^^^^     ^    ^^^^^^^^^^        ^ 

See the regex demo

Note that if you do not need the string itself, .+ is redundant and can be removed. You might want to re-adjust the borders of the named capture groups if necessary.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563