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