6

I am trying to write a lex pattern that will allow me to recognize IPV6 addresses including IPV6 addresses with CIDR notation.The pattern that I use is given below.

IPV4ADDRESS_CIDR [ \t]*(((2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?)\.){3}(2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?)(\/(3[012]|[12]?[0-9])))[ \t]*
IPV4ADDRESS      [ \t]*(([[:digit:]]{1,3}"."){3}([[:digit:]]{1,3}))[ \t]*
hex4             ([[:xdigit:]]{1,4})
hexseq           ({hex4}(:{hex4})*)
hexpart          ({hexseq}|({hexseq}::({hexseq}?))|::{hexseq})
IPV6ADDRESS      [ \t]*({hexpart}(":"{IPV4ADDRESS})?)[ \t]*
IPV6ADDRESS_CIDR [ \t]*(IPV6ADDRESS)(\/(1[01][0-9]|12[0-8]|[0-9]{1,2}))[ \t]*

The regular expression for IPV6ADDRESS_CIDR is not working as expected.I am testing with

2001:1234::5678:5:6:7:8/64 

It doesn't seem to recognize it properly.Am I making some mistake here?

IRSHAD
  • 2,855
  • 30
  • 39
liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 1
    First off, your IPV6ADDRESS_CIDR node only seems to match prefix lengths ≤32 whereas it needs to match anything between 0 and 128. Second, you aren't counting the number of hex groups, so you are going to allow some invalid things like 1:2:3:4:5:6:7:8:9 (too many parts). Finally, there are a few special cases that you miss, like addresses starting with :: or ending with :: (including the all-zeroes address "::" itself). – Celada Dec 27 '12 at 21:40
  • I have edited the regular expression to extend the prefix length to <= 128 instead of <=32. – liv2hak Dec 27 '12 at 21:56
  • I have edited the regular expression to extend the prefix length to <= 128 instead of <=32.I will fix the group count problem later.currently it takes just the first 8 groups.Now Still I am not able get the CIDR pattern to work properly. – liv2hak Dec 27 '12 at 22:05
  • 1
    You don't seem to be properly quoting all the literal characters (`::` and various digits), or for that matter, putting all your definition names inside `{}` (`IPV6ADDRESS`). Does that pattern even compile? By the way, it's probably a better idea to skip white space in a separate rule; adding whitespace to the ends of every token usually doesn't work out very well. – rici Dec 29 '12 at 06:09

1 Answers1

0

I have managed to get the regular expression for IPV6 address as follows.

IPV6ADDRESS_CIDR [ \t]{IPV6ADDRESS}(/(1[01][0-9]|12[0-8]|[0-9]{1,2}))[ \t]

I have tested the above and it is working.thanks.

liv2hak
  • 14,472
  • 53
  • 157
  • 270