Lokesh
Lokesh

Reputation: 3112

Regex not working as expected when combined with a dot

I am writing a regex to recognize IP address of the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3. I know this regex is easily available on Internet but I am writing it on my own for practice.

First I wrote the regex for A as follows:

a = ^(^0{0,2}\d|^0{0,1}\d\d|[0-1]\d\d|2[0-4]\d|25[0-5])$

It works as expected, then I wrote it for A.B as follows:

ab = ^(^0{0,2}\d|^0{0,1}\d\d|[0-1]\d\d|2[0-4]\d|25[0-5])\.
      (^0{0,2}\d|^0{0,1}\d\d|[0-1]\d\d|2[0-4]\d|25[0-5])$

But somehow it isn't working as expected. It's not recognizing strings like "2.3" but recognizing "2.003". This is very weird. I have spent hours figuring it out but have totally given up now. Please help me with this.

Upvotes: 1

Views: 138

Answers (1)

user2384183
user2384183

Reputation:

As @Jorge pointed out in the comments, the ^ character matches the start of a string/line, which can occur for A, as it is presumably the first group of characters in the line, but cannot occur for B, since it will always be preceded by A. This is why it could match 003 (through the subpattern [0-1]\d\d), but it couldn't match 3 through the subpattern ^0{0,2}\d.

Remove the superfluous ^s, and you should get the desired behavior:

ab = ^(0{0,2}\d|0{0,1}\d\d|[0-1]\d\d|2[0-4]\d|25[0-5])\.
      (0{0,2}\d|0{0,1}\d\d|[0-1]\d\d|2[0-4]\d|25[0-5])

Upvotes: 2

Related Questions