Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any equivalent to the Perl regexes' \K backslash sequence in Python?

Tags:

python

regex

perl

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 write s/PAT1 \K PAT2/REPL/x instead of s/(PAT1) PAT2/${1}REPL/x or s/(?<=PAT1) PAT2/REPL/x.

Mnemonic: Keep.

Is there anything equivalent in Python?

like image 897
Eugene Yarmash Avatar asked Sep 23 '16 15:09

Eugene Yarmash


People also ask

How do you match a backslash in regex 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.

Does Python use Perl regex?

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.

What does backslash mean in Python regex?

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 ).

What does backslash D mean in regex?

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.


1 Answers

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'>
like image 161
Charles Duffy Avatar answered Oct 19 '22 11:10

Charles Duffy