I need to use ruby to find a MAC Address in some text. The format of the MAC address is in the text is XXXX.XXXX.XXXX
, where X
could be an upper or lower case letter from A-F, or a number from 0-9.
I then need to convert the said MAC into this format: XX:XX:XX:XX:XX:XX
.
The below code works when the MAC address in some_text
is a081.572c.9a04
, but doesn't work when it is 9c21.0adf.9a41
.
# Matches
some_text = "some text a081.572c.9a04 some other text ..."
mac_addr = some_text.scan(/.\d+[A-Fa-f0-9].\d+[A-Fa-f0-9].\d+[A-Fa-f0-9].\d+/)[0]
puts mac_addr.to_s.gsub('.','').scan(/\w{2}/).join(':')
=> a0:81:57:2c:9a:04
# Does not match
some_text = "some text 9c21.0adf.9a41 some other text ..."
mac_addr = some_text.scan(/.\d+[A-Fa-f0-9].\d+[A-Fa-f0-9].\d+[A-Fa-f0-9].\d+/)[0]
puts mac_addr.to_s.gsub('.','').scan(/\w{2}/).join(':')
=>
Why does this code match a081.572c.9a04
, but not 9c21.0adf.9a41
, when they both have the same character groups?