以下代码出了什么问题 – 我在评论中将其指向连字符,但为什么会导致错误?
import re
valid = re.compile(r"""[^
# non-characters
]""", re.VERBOSE)
Traceback (most recent call last):
File "valid.py", line 5, in
]""", 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
没有连字符的下一个段没有错误:
import re
valid = re.compile(r"""[^
# non characters !! no errors
]""", re.VERBOSE)
编辑:
添加到@nhahtdh的答案,字符串连接似乎是另一种以详细样式注释字符类的合理方法:
valid = re.compile( r"[^"
r"-" # C0 block first segment
r"u" # allow TAB U+0009, LF U+000A, and CR U+000D
r"-" # rest of C0
r"" # disallow DEL U+007F
r"-" # All C1 block
r"]" # don"t forget this!
r"""
| [0-9] # normal verbose style
| [a-z] # another term +++
""", re.VERBOSE)
解决方法:
根据文件(强调我的):
re.X
re.VERBOSE
This flag allows you to write regular expressions that look nicer. Whitespace within the pattern is ignored, except when in a character class or preceded by an unescaped backslash, and, when a line contains a "#’ neither in a character class or preceded by an unescaped backslash, all characters from the leftmost such "#’ through the end of the line are ignored.
基本上,您不能在字符类中进行注释,并且字符类中的空格被认为是重要的.
因为#在字符类中,所以它不作为注释起作用,并且字符类中的所有内容都被解析为字符类的一部分而没有异常(即使新行字符被解析为字符类的一部分).由于n-c是无效字符范围而引发错误.
编写表达式的有效方法是:
valid = re.compile(r"[^] # non-characters", re.VERBOSE)
以下是关于如何在解释冗长的字符类时进行注释的一个建议:
r"""
# LOTS is for foo
# _ is a special fiz
# OF-LITERAL is for bar
[^LOTS_OF-LITERAL]
"""
标签:python,regex
来源: https://codeday.me/bug/20190529/1176744.html