Find String Not Preceded By Other String
I want to get only ['bar'] here: >>> re.findall(r'(?
Solution 1:
The current regex matches oo in foo because oo( is not preceded with "def ".
To stop the pattern from matching inside a word, you may use a a word boundary, \b and the fix might look like r"\b(?<!\bdef )([a-zA-Z0-9.]+?)\(".
Note that identifiers can be matched with [a-zA-Z_][a-zA-Z0-9_], so your pattern can be enhanced like
re.findall(r'\b(?<!\bdef\s)([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)\(', s, re.A)
Note that re.A or re.ASCII will make \w match ASCII only letters, digits and _.
See the regex demo.
Details
\b- a word boundary(?<!\bdef\s)- nodef+ space allowed immediately to the left of the current location([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)- Capturing group 1 (its value will be the result ofre.findallcall):[a-zA-Z_]- an ASCII letter or_\w*- 1+ word chars(?:- start of a non-capturing group matching a sequence of...\.- a dot[a-zA-Z_]- an ASCII letter or_\w*- 1+ word chars
)*- ... zero or more times\(- a(char.
Post a Comment for "Find String Not Preceded By Other String"