Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression working in Python3.5.2, not 3.7.4

I am using a modified library I found on the web, for parsing .stl files. It's been working wonderfully so far, in non-Anaconda Python 3.5.2. I recently had to upgrade to Anaconda Python3.7.4. The following line works well in 3.5, but throws an exception in 3.7.4

re.compile(r'[-+]?[0-9]*\.?[0-9]+(\e[-+]?[0-9]+)?')

What is the reason for this?
The exception is:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-2-08df04adea1a> in <module>
----> 1 re.compile(r'[-+]?[0-9]*\.?[0-9]+(\e[-+]?[0-9]+)?')

~/anaconda3/lib/python3.7/re.py in compile(pattern, flags)
    232 def compile(pattern, flags=0):
    233     "Compile a regular expression pattern, returning a Pattern object."
--> 234     return _compile(pattern, flags)
    235 
    236 def purge():

~/anaconda3/lib/python3.7/re.py in _compile(pattern, flags)
    284     if not sre_compile.isstring(pattern):
    285         raise TypeError("first argument must be string or compiled pattern")
--> 286     p = sre_compile.compile(pattern, flags)
    287     if not (flags & DEBUG):
    288         if len(_cache) >= _MAXCACHE:

~/anaconda3/lib/python3.7/sre_compile.py in compile(p, flags)
    762     if isstring(p):
    763         pattern = p
--> 764         p = sre_parse.parse(p, flags)
    765     else:
    766         pattern = None

~/anaconda3/lib/python3.7/sre_parse.py in parse(str, flags, pattern)
    928 
    929     try:
--> 930         p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
    931     except Verbose:
    932         # the VERBOSE flag was switched on inside the pattern.  to be

~/anaconda3/lib/python3.7/sre_parse.py in _parse_sub(source, state, verbose, nested)
    424     while True:
    425         itemsappend(_parse(source, state, verbose, nested + 1,
--> 426                            not nested and not items))
    427         if not sourcematch("|"):
    428             break

~/anaconda3/lib/python3.7/sre_parse.py in _parse(source, state, verbose, nested, first)
    814             sub_verbose = ((verbose or (add_flags & SRE_FLAG_VERBOSE)) and
    815                            not (del_flags & SRE_FLAG_VERBOSE))
--> 816             p = _parse_sub(source, state, sub_verbose, nested + 1)
    817             if not source.match(")"):
    818                 raise source.error("missing ), unterminated subpattern",

~/anaconda3/lib/python3.7/sre_parse.py in _parse_sub(source, state, verbose, nested)
    424     while True:
    425         itemsappend(_parse(source, state, verbose, nested + 1,
--> 426                            not nested and not items))
    427         if not sourcematch("|"):
    428             break

~/anaconda3/lib/python3.7/sre_parse.py in _parse(source, state, verbose, nested, first)
    505 
    506         if this[0] == "\\":
--> 507             code = _escape(source, this, state)
    508             subpatternappend(code)
    509 

~/anaconda3/lib/python3.7/sre_parse.py in _escape(source, escape, state)
    400         if len(escape) == 2:
    401             if c in ASCIILETTERS:
--> 402                 raise source.error("bad escape %s" % escape, len(escape))
    403             return LITERAL, ord(escape[1])
    404     except ValueError:

error: bad escape \e at position 21
like image 770
user650261 Avatar asked Aug 23 '26 07:08

user650261


1 Answers

The issue you're experiencing is due to the escape sequence you added of \e. Changing that to e will solve your issue.

So why did this happen after you upgraded from python 3.5.2 to python 3.7.4?

Well thanks to Wiktor for pointing this out in a comment under this question pointing me in the right direction.

According to What's new in Python 3.7 - API and Feature Removals:

Unknown escapes consisting of \ and an ASCII letter in replacement templates for re.sub() were deprecated in Python 3.5, and will now cause an error

Also noted in 3.7 - Regular Expression Syntax (the very last line in that section):

Changed in version 3.6: Unknown escapes consisting of '\' and an ASCII letter now are errors.


You can also see another answer (by Wiktor) with similar information here: The “\z” anchor not working in Python regex.

like image 190
ctwheels Avatar answered Aug 24 '26 21:08

ctwheels