regular expression on mac address

Posted by jackylee26 on 2016-05-17 18:28

Hi,
I'm meeting a problem with a regular expression.

I used tcl to access from mib to get my mac address, but it only print mac without colon, like this
XX XX XX XX XX XX i wants to put colon like this XX:XX:XX:XX:XX:XX. So i used regular expression

but it will have problem. It will print XX:XX:XX:XX:XX:XX: ==>i wish to print like this xx:xx:xx:xx:xx:xx

code as below:

set mac [lindex [split [snmp_get -t 5 -Oqv $IP private .1.3.6.1.2.1.1.2.1.1.2] "\""] 1 ]

regexp {([\w]+\s[\w]+\s[\w]+\s[\w]+\s[\w]+\s[\w]+)} $mac match ::txt11

set ::txt6 "[regsub -all {\s} $mac {:}]"

Do anyone how why is it.

shas-0 | Tue, 2017-09-12 22:39

Hi jacklee26

you could try a string trim on the mac variable before the regsub.
Doesn't you ::txt11 contain the timed mac address?

set mac [lindex [split [snmp_get -t 5 -Oqv $IP private .1.3.6.1.2.1.1.2.1.1.2] "\""] 1 ]

regexp {([\w]+\s[\w]+\s[\w]+\s[\w]+\s[\w]+\s[\w]+)} $mac match ::txt11

#Lets see whats in mac and ::txt11
puts "\"$mac\" - \"$::txt11\""

set mac [ string trim $mac]

set ::txt6 "[regsub -all {\s} $mac {:}]"