"?:"在Python正则表达式中意味着什么?

Har*_*ari 7 python regex

下面是Python正则表达式.它的?:意思是什么?表达总体上做了什么?它如何匹配MAC地址,如" 00:07:32:12:ac:de:ef"?

re.compile(([\dA-Fa-f]{2}(?:[:-][\dA-Fa-f]{2}){5}), string)  
Run Code Online (Sandbox Code Playgroud)

Jon*_*ler 8

(?:...)表示一组非捕获分组括号.

通常,当您(...)使用正则表达式编写时,它会"捕获"匹配的材料.当您使用非捕获版本时,它不会捕获.

你可以在通过配套的各部分正则表达式使用的方法re包正则表达式针对特定的字符串匹配之后.


这个正则表达式如何匹配MAC地址"00:07:32:12:ac:de:ef"?

这与你最初提出的问题不同.但是,正则表达式部分是:

([\dA-Fa-f]{2}(?:[:-][\dA-Fa-f]{2}){5})
Run Code Online (Sandbox Code Playgroud)

最外面的一对括号是捕获括号; 当您成功使用正则表达式对字符串时,它们所包围的内容将可用.

[\dA-Fa-f]{2}部分匹配一对数字(\d)或十六进制数字A-Fa-f],{2}然后是非捕获分组,其中匹配的材料是冒号或破折号(:-),后跟另一对十六进制数字,整数重复5倍.

p = re.compile(([\dA-Fa-f]{2}(?:[:-][\dA-Fa-f]{2}){5}))
m = p.match("00:07:32:12:ac:de:ef")
if m:
    m.group(1)
Run Code Online (Sandbox Code Playgroud)

最后一行应该打印字符串"00:07:32:12:ac:de",因为这是第一组6对十六进制数字(在字符串中总共七对中).实际上,外部分组括号是多余的,如果省略,则m.group(0)可以工作(即使它们也可以工作).如果您需要匹配7对,那么您将5更改为6.如果您需要拒绝它们,那么您将锚点放入正则表达式:

p = re.compile(^([\dA-Fa-f]{2}(?:[:-][\dA-Fa-f]{2}){5})$)
Run Code Online (Sandbox Code Playgroud)

插入符号^匹配字符串的开头; 美元$符合字符串的结尾.使用5,这与您的样本字符串不匹配.用6代替5,它会匹配你的字符串.


Cyl*_*ian 6

使用?:as (?:...)会使组在替换期间不捕获.在找到它时没有任何意义.

您的RegEx意味着

r"""
(                   # Match the regular expression below and capture its match into backreference number 1
   [\dA-Fa-f]          # Match a single character present in the list below
                          # A single digit 0..9
                          # A character in the range between “A” and “F”
                          # A character in the range between “a” and “f”
      {2}                 # Exactly 2 times
   (?:                 # Match the regular expression below
      [:-]                # Match a single character present in the list below
                             # The character “:”
                             # The character “-”
      [\dA-Fa-f]          # Match a single character present in the list below
                             # A single digit 0..9
                             # A character in the range between “A” and “F”
                             # A character in the range between “a” and “f”
         {2}                 # Exactly 2 times
   ){5}                # Exactly 5 times
)
"""
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.