Perl's regular expressions have the \K
backslash sequence:
\K
This appeared in perl 5.10.0. Anything matched left of\K
is not included in$&
, and will not be replaced if the pattern is used in a substitution. This lets you writes/PAT1 \K PAT2/REPL/x
instead ofs/(PAT1) PAT2/${1}REPL/x
ors/(?<=PAT1) PAT2/REPL/x
.Mnemonic: Keep.
Is there anything equivalent in Python?
In short, to match a literal backslash, one has to write '\\\\' as the RE string, because the regular expression must be "\\", and each backslash must be expressed as "\\" inside a regular Python string literal.
Python supports essentially the same regular expression syntax as Perl, as far as the regular expressions themselves. However, the syntax for using regular expressions is substantially different.
In Python, the backslash( \ ) is a special character. If you use the backslash in front of another character, it changes the meaning of that character. For example, the t is a literal character. But if you use the backslash character in front of the letter t , it'll become the tab character ( \t ).
Within a regex a \ has a special meaning, e.g. \d means a decimal digit. If you add a backslash in front of the backslash this special meaning gets lost.
The proposed replacement to the Python re
module, available from pypi
under the name regex
, has this feature. Its canonical source repository and bug tracker are in bitbucket.
This was added in late 2015, in ticket 151; taking an example of its use from that ticket:
import regex as mrab >>> bsk = mrab.compile(r'start=>\K.*') >>> print(bsk.search('boring stuff start=>interesting stuff')) <regex.Match object; span=(20, 37), match='interesting stuff'>
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