python注释符号错误-python – 详细的正则表达式注释中的连字符会导致错误

以下代码出了什么问题 – 我在评论中将其指向连字符,但为什么会导致错误?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值