I have some python code writen in an older version of python(2.x) and I struggle to make it work. I\'m using python 3.4
_eng_word = ur\"[a-zA-Z][a-zA-Z0-9\'.]*\
Indeed, Python 3.4 only supports u'...'
(to support code that needs to run on both Python 2 and 3) and r'....'
, but not both. That's because the semantics of how ur'..'
works in Python 2 are different from how ur'..'
would work in Python 3 (in Python 2, \uhhhh
and \Uhhhhhhhh
escapes still are processed, in Python 3 a `r'...' string would not).
Note that in this specific case there is no difference between the raw string literal and the regular! You can just use:
_eng_word = u"[a-zA-Z][a-zA-Z0-9'.]*"
and it'll work in both Python 2 and 3.
For cases where a raw string literal does matter, you could decode the raw string from raw_unicode_escape
on Python 2, catching the AttributeError
on Python 3:
_eng_word = r"[a-zA-Z][a-zA-Z0-9'.]*"
try:
# Python 2
_eng_word = _eng_word.decode('raw_unicode_escape')
except AttributeError:
# Python 3
pass
If you are writing Python 3 code only (so it doesn't have to run on Python 2 anymore), just drop the u
entirely:
_eng_word = r"[a-zA-Z][a-zA-Z0-9'.]*"