1

所以我对以下代码有疑问:

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})?)?"部分之外,代码的作用对我来说相对清楚,我只是不明白它的作用以及它是如何工作的,我确实检查了谷歌开发人员 codeschool 视频,我还检查了 python 文档,但是当我的目标是去除 IRC 消息的颜色和其他各种格式时,那么这部分在(如果可能的话)外行术语中到底做了什么。

我在线程中发现了这个: 如何去除 mIRC 用户使用的颜色代码?

(?: ... ) 表示忘记存储在括号中找到的内容(因为我们不需要反向引用它),?表示匹配 0 或 1,{n,m} 表示将 n 匹配到前一个分组的 m。最后,\d 表示匹配 [0-9]。

但我并没有真正理解它=/

4

2 回答 2

2

http://myregextester.com来救援!

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 位数字组成的组。

所以以下将匹配(假设整行匹配):

12
1
20
10,2
22,3
12,0
14,20

但以下不会:

200
a,b
!123p9
1000,2000
于 2013-10-31T00:24:01.280 回答
0
(?:\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
于 2013-10-31T00:22:43.023 回答