python version 3.4 does not support a 'ur' prefix

前端 未结 3 609
孤街浪徒
孤街浪徒 2021-02-07 09:26

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\'.]*\         


        
3条回答
  •  逝去的感伤
    2021-02-07 09:47

    This table compares (some of) the different string literal prefixes in Python 2(.7) and 3(.4+):

    As you can see, in Python 3 there's no way to have a literal that doesn't process escapes, but does process unicode literals. To get such a string with code that works in both Python 2 and 3, use:

    br"[a-zA-Z][a-zA-Z0-9'.]*".decode('raw_unicode_escape')
    

    Actually, your example is not very good, since it doesn't have any unicode literals, or escape sequences. A better example would be:

    br"[\u03b1-\u03c9\u0391-\u03a9][\t'.]*".decode('raw_unicode_escape')
    

    In python 2:

    >>> br"[\u03b1-\u03c9\u0391-\u03a9][\t'.]*".decode('raw_unicode_escape')
    u"[\u03b1-\u03c9\u0391-\u03a9][\\t'.]*"
    

    In Python 3:

    >>> br"[\u03b1-\u03c9\u0391-\u03a9][\t'.]*".decode('raw_unicode_escape')
    "[α-ωΑ-Ω][\\t'.]*"
    

    Which is really the same thing.

提交回复
热议问题