Skip to content Skip to sidebar Skip to footer

Python 2 String Expression Not Recognised Using Python 3

I have a python script containing this regex: expression1 = ur'(.*?),\s(.*)\s(sold(?: post-exercise)?|bought|purchased|awarded|exercised|transferred in|transferred out|re-invested)

Solution 1:

Python 2 ur strings had a weird incompatibility with Python 3, where \u and \U escapes would still be processed instead of being left "raw". When the u prefix was reintroduced to Python 3 in the 3.3 revision, an explicit decision was made to exclude the ur combination, rather than have inconsistent behavior.

If you want a raw Unicode string that works in both Python 2 and 3, you'll need a workaround. Possibilities include using a br raw bytestring and converting it to Unicode with an appropriate codec, or using from __future__ import unicode_literals and using the plain r prefix. Be careful about \u and \U escapes.

Solution 2:

All strings in Python 3 are unicode so the leading u is not needed. Simply remove that u to make it work with Python 3.

Solution 3:

I would say the best way to have Python 2/3 interoperability is to use:

from __future__ import unicode_literals

Then simply remove the u. If you explicitely need bytes, then a b"these are byte-strings" will work for both 2/3.

Post a Comment for "Python 2 String Expression Not Recognised Using Python 3"