Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid syntax using regular expression in python 3.4

I am using the following expression in python 3.4 it gives syntax error but the same code works in python 2.7

block = re.compile(ur'DATA\(value\)[\S ]+\s((?:(?![^\n]+DATA\(value2\)).)*)', re.IGNORECASE | re.DOTALL)

I am unsure what is the real issue in this expression

like image 885
user89 Avatar asked May 13 '26 17:05

user89


1 Answers

Python 3 has no ur'...' raw unicode string syntax. Use r'...' instead:

block = re.compile(
    r'DATA\(value\)[\S ]+\s((?:(?![^\n]+DATA\(value2\)).)*)',
    re.IGNORECASE | re.DOTALL)

If you need to create cross-Python compatible code, you'll have to use conditional code that'll decode the byte string produced by r'...' to a unicode object only on Python 2. A module like six can help with that:

from six import u

block = re.compile(
    u(r'DATA\(value\)[\S ]+\s((?:(?![^\n]+DATA\(value2\)).)*)'),
    re.IGNORECASE | re.DOTALL)

Or you can create your own compatibility layer; for local one-off tests, you could see if there is a str.decode() method present:

_block_pattern = r'DATA\(value\)[\S ]+\s((?:(?![^\n]+DATA\(value2\)).)*)'
if hasattr(_block_pattern, 'decode'):
    # Python 2, decode to unicode first
    _block_pattern = _block_pattern.decode('ascii')
block = re.compile(_block_pattern, re.IGNORECASE | re.DOTALL)
like image 104
Martijn Pieters Avatar answered May 15 '26 05:05

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!