Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two question about python regular expressions

Tags:

python

c#

regex

Q1. why we can not use word boundary and back reference without using r at start of regex? e.g. '\b[a-z]{5}\d{3}\b' this not works but this r'\b[a-z]{5}\d{3}\b' works

Q2. why python does not support variable length negative look behind assertions while it supports variable length negative look ahead assertion, c# support both and i think it is an excellent feature to have also variable length negative look behind in python.

please clear these two concepts. thanks

like image 685
Aamir Rind Avatar asked Sep 14 '26 08:09

Aamir Rind


1 Answers

It does work without raw strings:

'\\b[a-z]{5}\\d{3}\\b'

You just need to double escape the special chars (actually, what you do is escape all backslashes).

Variable length assertions are one of those features that some implementations support and some don't. Check out the regex module on PyPI for a version with more features and better unicode support, which may eventually replace the standard library re.

Edit: To make the version from your comment work without raw strings, use:

re.sub('[a-z]+(\d+)', '\\1', string)

Again, Python interprets backslashes. it thinks \1 means a byte value of 1. If you actually mean \1, you need to escape the backslash by doing \\1, or use raw strings.

Edit 2: Adding the link from @Nate's comment to the list of Python escape sequences.

like image 74
agf Avatar answered Sep 16 '26 23:09

agf