简体   繁体   中英

scala IPv6 regex matching and group extraction

I am trying to match the ipv6 regex from a string like:

3111105 fe88::777:2333:e89:12f3,ff42::9,tcp/0/0

of which I want to extract the two IPv6 addresses.

I have the regex as below:

(([a-f0-9]{0,4}:)+[a-f0-9]{1,4}),(([a-f0-9]{0,4}:)+[a-f0-9]{1,4}),([a-zA-Z0-9]+)(.*)

But I get a part of the first ip in group(1) and part of second ip in group 4.

This is what I get:

group(0)- (fe88::777:2333:e89:12f3)

group(1)- (e89:)

group(2)- (ff42::9)

group(3)- (:)

group(4)- (tcp)

group(5)- (/0/0)

However expected is:

group(0)- (fe88::777:2333:e89:12f3)

group(1)- (ff42::9)

group(2)- (tcp)

group(3)- (/0/0)

Any suggestions?

The regex was missing on the optional character which is a ":" for the first part.

Hence following regex would capture the groups correctly:

((?:[0-9a-f]{0,4}:)+[0-9a-f]{0,4})+,((?:[0-9a-f]{0,4}:)+[0-9a-f]{0,4})+,([a-zA-Z0-9]+)(.*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM