What is wrong with the following code - I pointed it to a hyphen in the comment, but why should this cause an error?
import re valid = re.compile(r'''[^ \uFFFE\uFFFF # non-characters ]''', re.VERBOSE) Traceback (most recent call last): File "valid.py", line 5, in <module> ]''', re.VERBOSE) File "/usr/local/lib/python3.3/re.py", line 214, in compile return _compile(pattern, flags) File "/usr/local/lib/python3.3/re.py", line 281, in _compile p = sre_compile.compile(pattern, flags) File "/usr/local/lib/python3.3/sre_compile.py", line 494, in compile p = sre_parse.parse(p, flags) File "/usr/local/lib/python3.3/sre_parse.py", line 748, in parse p = _parse_sub(source, pattern, 0) File "/usr/local/lib/python3.3/sre_parse.py", line 360, in _parse_sub itemsappend(_parse(source, state)) File "/usr/local/lib/python3.3/sre_parse.py", line 506, in _parse raise error("bad character range") sre_constants.error: bad character range
The following segment without a hyphen does not contain errors:
import re valid = re.compile(r'''[^ \uFFFE\uFFFF # non characters !! no errors ]''', re.VERBOSE)
Edit:
Adding @nhahtdh to the answer, string concatenation seems to be another reasonable way to comment on verbose-style character classes:
valid = re.compile( r'[^' r'\u0000-\u0008'
source share