问题
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
回答1:
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)
来源:https://stackoverflow.com/questions/27482273/invalid-syntax-using-regular-expression-in-python-3-4