Invalid syntax using regular expression in python 3.4

夙愿已清 提交于 2019-12-10 10:07:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!