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)
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).
(?=...)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With