classification
Title: Regex becomes invalid in python 3.6
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Chris Seto2, ezio.melotti, mrabarnett, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-04-27 14:07 by Chris Seto2, last changed 2017-04-27 15:06 by Chris Seto2. This issue is now closed.

Messages (3)
msg292445 - (view) Author: Chris Seto (Chris Seto2) Date: 2017-04-27 14:07
Expected behavior:
~ ❯❯❯ pyenv shell 3.5.2
~ ❯❯❯ python --version
Python 3.5.2
~ ❯❯❯ python
Python 3.5.2 (default, Oct 24 2016, 00:12:20)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.compile('[^\B]')
re.compile('[^\\B]')
>>> re.compile(r'[^\B]')
re.compile('[^\\B]')

Actual:
~ ❯❯❯ pyenv shell 3.6.0
~ ❯❯❯ python --version
Python 3.6.0
~ ❯❯❯ python
Python 3.6.0 (default, Apr 26 2017, 17:24:07)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.compile('[^\B]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/re.py", line 233, in compile
    return _compile(pattern, flags)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_parse.py", line 856, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_parse.py", line 415, in _parse_sub
    itemsappend(_parse(source, state, verbose))
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_parse.py", line 526, in _parse
    code1 = _class_escape(source, this)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_parse.py", line 336, in _class_escape
    raise source.error('bad escape %s' % escape, len(escape))
sre_constants.error: bad escape \B at position 2

>>> re.compile(r'[^\B]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/re.py", line 233, in compile
    return _compile(pattern, flags)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_parse.py", line 856, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_parse.py", line 415, in _parse_sub
    itemsappend(_parse(source, state, verbose))
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_parse.py", line 526, in _parse
    code1 = _class_escape(source, this)
  File "/Users/chrisseto/.pyenv/versions/3.6.0/lib/python3.6/sre_parse.py", line 336, in _class_escape
    raise source.error('bad escape %s' % escape, len(escape))
sre_constants.error: bad escape \B at position 2
msg292448 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-27 14:59
This is ambiguous syntax. Unknown escapes consisting of '\' and ASCII letter were deprecated in 3.5 (run the interpreter with the option -Wa to see all warnings) and are errors since 3.6. Note that the string literal '[^\B]' itself produces a DeprecationWarning in 3.6.
msg292449 - (view) Author: Chris Seto (Chris Seto2) Date: 2017-04-27 15:06
Thank you for pointing that out to me!
History
Date User Action Args
2017-04-27 15:06:49Chris Seto2setmessages: + msg292449
2017-04-27 14:59:38serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg292448

resolution: not a bug
stage: resolved
2017-04-27 14:07:54Chris Seto2create