>> m = re.search("(?>> m.group(0) 'Batman' Why does...">

Could you explain why this regex doesn't work?

>>> d = "Batman,Superman"
>>> m = re.search("(?<!Bat)\w+",d)
>>> m.group(0)
'Batman'

Why doesn't group (0) match Superman? This tutorial says:

(? <! a) b matches "b", which is not which is preceded by "a", using a negative look back

+3
source share
5 answers

, ( , , ). , , , , . , .

0 (.. B Batman) , Bat - , \w+ Batman (, - ).

. .


, , - :

\b(?!Bat)\w+

(\b) 1 , , Bat. lookahead, lookbehind, lookbehind , ; , , , , lookbehind .

1 , \w \w (.. [A-Za-z0-9_] , ^ $ anchors). , .

+1

Batman Bat, . , , Superman; , , RE , , .

, : Batman, m, RE , ( an), , Bat.

+6

- (\w+), "Bat". - . ( , lookbehind .)

+1

To do what you want, you must restrict the regex to match 'man'; otherwise, as others have noted, \weagerly matches anyone, including 'Batman'. How in:

>>> re.search("\w+(?<!Bat)man","Batman,Superman").group(0)
'Superman'
+1
source

All Articles