What does the "?:" In Python regex mean?

The following is a Python regex. What does it mean ?:in it? What does the expression as a whole do? How does it match the MAC address, for example, " 00:07:32:12:ac:de:ef"?

re.compile(([\dA-Fa-f]{2}(?:[:-][\dA-Fa-f]{2}){5}), string)  
+5
source share
3 answers

This (?:...)means a set of non-converting brackets for grouping.

Usually, when you write (...)in a regular expression, it "captures" the matched material. When you use the non-converting version, it is not fixed.

You can get the various parts matching regex using methods in reafter the regex matches a specific string.


MAC- "00: 07: 32: 12: ac: de: ef"?

, . , :

([\dA-Fa-f]{2}(?:[:-][\dA-Fa-f]{2}){5})

; , , , .

[\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)

"00: 07: 32: 12: ac: de", ( ). , , , m.group(0) ( ). 7 , 5 6. , :

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

^ ; $ . 5, . 6 5 .

+7

?:, (?:...), . .

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
)
"""

, .

+5

(?:...) . .

+1

All Articles