Python正则表达式重新编译澄清
因此,我对以下代码有一个问题:Python正则表达式重新编译澄清,python,regex,syntax,Python,Regex,Syntax,因此,我对以下代码有一个问题: def OnChanMsg(self, nick, channel, message): if 'Username' in nick.GetNick(): stripped = message.s.strip() #strips leading and lagging whitespaces regex = re.compile("\x1f|\x02|\x12|\x0f|\x16|\x03(?:\d{
def OnChanMsg(self, nick, channel, message):
if 'Username' in nick.GetNick():
stripped = message.s.strip() #strips leading and lagging whitespaces
regex = re.compile("\x1f|\x02|\x12|\x0f|\x16|\x03(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE) #recompiles the mesasge minus colorcodes, bold etc
ircstripped = regex.sub("", stripped)
all = re.findall(r'test\ for\ (.*)\: ->\ (.*)\ \((.*)\)\ -\ \((.*)\)\ - \((.*)\).*', ircstripped)
因此,我的问题如下:
1) 除了“(?:\d{1,2}(?:,\d{1,2})?”,“
部分,我只是不明白它的功能和工作原理,我查看了google开发者代码学校的视频,我还查看了python文档,但是,当我的目标是剥离IRC消息的颜色和其他各种格式时,那么这一部分在(如果可能的话)外行术语中到底做了什么
我在线里面发现了这个:
(?:…)说忘记存储在
括号(因为我们不需要反向引用它)?表示匹配0
或1和{n,m}表示将前一分组的n与m匹配。
最后,\d表示匹配[0-9]
但我真的不明白=/
(?:\d{1,2}(?:,\d{1,2})?)?
就是零,一个或两个数字,有1或2个数字,用逗号分隔
(?:\d{1,2}(?:,\d{1,2})?)? = (?:\d{1,2}(?:,\d{1,2})?) followed by ?
= the whole thing is optional
(?:\d{1,2}(?:,\d{1,2})?) = \d{1,2}(?:,\d{1,2})? in a group that is not stored
\d{1,2}(?:,\d{1,2})? = \d{1,2} followed by (?:,\d{1,2})?
= 1 or 2 digits followed by (?:,\d{1,2})?
(?:,\d{1,2})? = (?:,\d{1,2}) followed by ?
= (?:,\d{1,2}) is optional
(?:,\d{1,2}) = ,\d{1,2} in a group that is not stored
,\d{1,2} = , (comma) followed by 1 or 2 digits
救命啊
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
因此,换句话说:可以选择捕获1-2个数字,也可以选择后跟一个由逗号和1-2个数字组成的组。
因此,以下内容将匹配(假设整行匹配):
但以下情况不会:
200
a,b
!123p9
1000,2000
非常感谢:)谢谢,那是一个很棒的网站:)
200
a,b
!123p9
1000,2000