0

Does anyone have any suggestions for my regex for IPv4. This does not completely validate an IPv4 address.

IPV4="^(|[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})\|"

1.1.1.1 is ok, but 999.1.1.1 also validates :(

Thanks for the input

* UPDATE *

Based on DopeGhoti's input, here is where im at so far. I'm running into issues adding this into my larger regex. I am setting this regex equal to a variable and then combining it with another expression for IPv6 as well. There are a few things that I have added to the regex. For example a (|insert DopeGhoti regex here). This allows me to accept an empty IPv4 address too. I also added a \| at the end because the two addresses being checked by the one big regex are | delimited.

IPV4="^(|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))\|"
REGEX="$IPV4$IPV6"

This process works with my simplier IPv4 regex. Something in the new regex is breaking it.

* UPDATE *

Ok weird. I tested the following in rubular:

http://rubular.com/r/c6J3sESDyN

^(|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))\|$

This seems to work. Maybe I have another internal issue. Checking now.

Atomiklan
  • 5,164
  • 11
  • 40
  • 62

3 Answers3

1

Have you tried to escape the dot char ?

 # ^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})

 ^ 
 (
      [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} 
 )
0

You could try (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)).

DopeGhoti
  • 757
  • 7
  • 15
  • This is very very close. – Atomiklan Jan 16 '14 at 20:03
  • I edited this and tested it against several good and bad "addresses", this seems to very nearly work as intended. It will still match `0.1.2.3` for example. – DopeGhoti Jan 16 '14 at 20:06
  • Unfortunately 255.255.255.257 still validates. – Atomiklan Jan 16 '14 at 20:15
  • Not when I [test](http://i.imgur.com/yOTN4iZ.png)? – DopeGhoti Jan 16 '14 at 20:20
  • 1
    Try it in here http://rubular.com/ – Atomiklan Jan 16 '14 at 20:36
  • I did, [here](http://rubular.com/r/5xCdazWxeA). If catching it as a substring is the problem, add `\b` at the start and end of the expression. Also, thanks for Rubular, I'm adding that to my toolkit. (: – DopeGhoti Jan 16 '14 at 20:41
  • Actually, if someone placed a 192.168.268.13 somewhere, I'd say it _looks_ like an IP number. So maybe the regexp should match even that (so actually, 999.999.999.999 should maybe, too) but a value-checker afterwards should reject it with a better error message than just "does not look like an IP number". – Alfe Jan 16 '14 at 21:00
  • Well, now you're just moving the goalposts; you asked for complete IP4 address validation. If you just want to match the numerical pattern without caring if it's actually an IP, you can use `([0-9]{1,3}\.){3}[0-9]{1,3}`. – DopeGhoti Jan 16 '14 at 21:07
  • That wasn't me lol. Lets not back track. – Atomiklan Jan 16 '14 at 21:31
  • Please see my update above – Atomiklan Jan 16 '14 at 21:38
0

Post-Update:

If the merged regexes are failing while working individually, that you could try distilling each of the two expressions, and then test with:

egrep "(${IP4PATTERN}|${IP6PATTERN})" /path/to/file
DopeGhoti
  • 757
  • 7
  • 15