簡體   English   中英

Python Regex重新編譯說明

[英]Python Regex re.compile clarification

所以我對以下代碼有疑問:

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 Developers Codeschool視頻,也檢查了python文檔,但是當我的目標是剝離其顏色和其他各種格式的IRC消息時,這部分用(如果可能)外行術語確切地做了什么。

我在線程內部發現了這一點: 如何剝離mIRC用戶使用的顏色代碼?

(?:...)表示忘記存儲括號中的內容(因為我們不需要反向引用它),? 表示匹配0或1,{n,m}表示將n匹配到先前分組的m。 最后,\\ d表示匹配[0-9]。

但是我不是真的得到它= /

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
(?:\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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM