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.
^inside the parenthesis as it already exists one outside it.