Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to understand python regex with ?=

Tags:

python

regex

I am not able to understand how these regex works and when they are used. I haven't found concrete examples of these regex on python website. I know sed awk but haven't used these type of regex there

(?=...)
(?<=...)
(?(id/name)yes-pattern|no-pattern)
like image 633
user2027303 Avatar asked Feb 01 '13 13:02

user2027303


People also ask

How do you use special characters in regex Python?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


1 Answers

(?=...) is a positive lookahead assertion. It matches if there is the part in parentheses after ?= matches at the current position, but it will not consume any characters for the match. E.g. the regex a(?=b) will match an a followed by a b, but won't return the b as part of the match.

(?<=...) is the same, but a look behind, i.e. it looks backwards. Again, it doesn't consume anything.

(?(id/name)yes-pattern|no-pattern) is a conditional. If the named group id/name matched, then the string must match yes-pattern at this point, otherwise no-pattern.

To be honest, though, those are quite advanced features and I cannot remember ever having used a conditional. Lookaround is more common, but often very constrained by the regex engines, e.g. lookbehind can only be done with fixed-length strings in many cases.

like image 72
Joey Avatar answered Nov 15 '22 02:11

Joey