Regexp for matching Interface address

48 views
Skip to first unread message

Sharan Guhan

unread,
Apr 1, 2021, 4:37:58 PM4/1/21
to golang-nuts
Hi Experts,

I am facing issues in getting the correct regexp for this use-case. Sorry for the 2nd question of the day :-( 

pci@0000:19:00.0        
pci@0000:19:00.1     
pci@0000:5e:00.2  
pci@0000:5e:00.3             
pci@0000:d8:00.0            
pci@0000:d8:00.1             

I need to fetch "0000:19:00.0" through "0000:d8:00.1" in a string.. I used the below regexp and was successful for "0000:19:00.0", but the same doesnt work for 5e or d8 and vice versa

r := regexp.MustCompile(`(\d*([a-f]*):\d*([a-f]*):\d+.\d+)`)
yields - [{ 0000:19:00.0} { 0000:19:00.1} { } { } { } { }]

(or)
r := regexp.MustCompile(`(\d*([a-f]*):\d+([a-f]*)|([a-f]+)\d+:\d+.\d+)`)
yields - [ 0000:19} { 0000:19} { d8:00.0} { d8:00.1} { d8:00.2} { d8:00.3}]

Please share your thoughts. 

Sharan

Axel Wagner

unread,
Apr 1, 2021, 4:49:23 PM4/1/21
to Sharan Guhan, golang-nuts
`strings.SplitN(s, "@", 2)[1]`

Of course with additional handling for the case that there is no @ or more than one @.
I know that "don't use regexp" may be a frustrating answer to this question, but IMO it's the correct one. Regular expressions are hard to maintain in production software.

If you really want to use a regexp, I'd use
`0{4}:[0-9a-f]{2}:0{2}\.[0-9]`
With the caveat, of course, that I have no idea if your example is exhaustive (i.e. you might have to replace the 0's with [0-9a-f] as well).



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAGOT8apn1BQVGQA3_z%2BrMk9fv0JrMB6QYwjnG%3Dq8g2TXBTkJuA%40mail.gmail.com.

Sharan Guhan

unread,
Apr 1, 2021, 5:27:55 PM4/1/21
to Axel Wagner, golang-nuts
Thanks much ! This code just works like a charm in my use-case.. I learnt we can do 

1.define # characters using "{4}"
2.Combine digits + alphabets using "[0-9a-f]"

Sharan
Reply all
Reply to author
Forward
0 new messages