Below is the Python regular expression. What does the ?: mean in it? What does the expression do overall? How does it match a MAC address such as “00:07:32:12:ac:de:ef“?
re.compile(([\dA-Fa-f]{2}(?:[:-][\dA-Fa-f]{2}){5}), string)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It
(?:...)means a set of non-capturing grouping parentheses.Normally, when you write
(...)in a regex, it ‘captures’ the matched material. When you use the non-capturing version, it doesn’t capture.You can get at the various parts matched by the regex using the methods in the
repackage after the regex matches against a particular string.That’s a different question from what you initially asked. However, the regex part is:
The outer most pair of parentheses are capturing parentheses; what they surround will be available when you use the regex against a string successfully.
The
[\dA-Fa-f]{2}part matches a digit (\d) or the hexadecimal digitsA-Fa-f], in a pair{2}, followed by a non-capturing grouping where the matched material is a colon or dash (:or-), followed by another pair of hex digits, with the whole repeated exactly 5 times.The last line should print the string "00:07:32:12:ac:de" because that is the first set of 6 pairs of hex digits (out of the seven pairs in total in the string). In fact, the outer grouping parentheses are redundant and if omitted,
m.group(0)would work (it works even with them). If you need to match 7 pairs, then you change the 5 into a 6. If you need to reject them, then you’d put anchors into the regex:The caret
^matches the start of string; the dollar$matches the end of string. With the 5, that would not match your sample string. With 6 in place of 5, it would match your string.