Invalid syntax using regular expression in python 3.4

£可爱£侵袭症+ 提交于 2019-12-05 21:00:23

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