0
^10\.\d+\.\d+\.\d+$/g

So I'm trying to create a RegEx that hits all the 10.x.x.x addresses. (So 10.0.0.0/8)

And I came up with what's above. When I test it on RegEx testing websites (I've tried a few) I get no match.

From what I understand:

It matches 10.x, then adds the previous and looks for x., then the previous, then x., until it hits the full 10.x.x.x. However it doesn't seem to work.

I'm sure that \d hits any digit.

I'm at a loss of why this is failing as it seems correct to me.

Could the RegEx tests be wrong, or am I missing something?

War10ck
  • 12,387
  • 7
  • 41
  • 54
Sean
  • 13
  • 3
  • by putting ^ in the begining and $ at the end it means that the whole line must be consumed by the match.. make sure there are no spaces or \r in your data. /g usually means to match all occurrences on the line, otherwise it will stop at the first one.. in this case it makes no sense since you pattern is explicitly states from the start ^ to the end $ of the line. – Rob Apr 18 '16 at 19:01
  • Upon testing it doesn't match any address that I try. (Just testing random 10. addresses) – Sean Apr 18 '16 at 19:02
  • 1
    Is *[this](https://regex101.com/r/uS1aY9/1)* what you want? – Mr. Meeseeks Apr 18 '16 at 19:03
  • 2
    http://stackoverflow.com/questions/2814002/private-ip-address-identifier-in-regular-expression – Wiktor Stribiżew Apr 18 '16 at 19:05
  • 1
    Could you post a link to your regexp test that shows it doesn't match? – Barmar Apr 18 '16 at 19:33

4 Answers4

3

The ^10\.\d+\.\d+\.\d+$ pattern matches a 10 at the start of a string, then a dot, followed with 1+ any digits (x 3 times) and then the end of a string. That means, this regex can match 10.100000.234567.345567.

The regex to match 10.xx.xx.xx IPv4 address can be written as

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

See the regex demo. This is an abridged and adapted IP address from Ultrapico Expresso.

Details:

  • ^ - start of string
  • 10 - literal 10
  • (?:\.(?:2[0-4]\d|25[0-5]|[01]?\d\d?)){3} - 3 sequences ({3}) of:
    • \. - a literal dot
    • (?:2[0-4]\d|25[0-5]|[01]?\d\d?) - 3 alternatives:
      • 2[0-4]\d - 2 followed with a digit from 0-4 range and one more digit (200-249)
      • 25[0-5] - 25 followed with a digit from 0-5 range (250-255)
      • [01]?\d\d? - 0 or 1 (optionally due to ?) followed with any 1 digit and an optional one digit (0-199)
  • $ - end of string
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Thank you. This is what I needed. – Sean Apr 18 '16 at 21:53
  • I could actually use some help, the final I came up with is: `/^10(?:\.(?:2[0-4]\d|25[0-5]|[01]?\d\d?)){3}@domain.com$/gm` And I can't get my filter to accept it, although it works in testers. What I think is happening is the Regex above is for PHP, and I need Javascript (not sure though), would you happen to know anything about this? All I can come up with is Javascript escaped characters potentially being the problem, but I'm not sure. I might contact support for the tool I'm using and ask them if they have specific regex formatting. – Sean Apr 22 '16 at 18:52
  • I am almost sure you can use [`^10([.](2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)){3}@domain[.]com$`](https://regex101.com/r/nU9eX0/1). In POSIX BRE, you need to escape the `\{3\}` – Wiktor Stribiżew Apr 22 '16 at 19:13
0

I would rather go with this regex to validate ip adresses

10.\d{1,3}.\d{1,3}.\d{1,3}

  • 3
    Note that this will still only perform a partial validation. For instance, `10.999.12.14` will pass validation but is not a legitimate IP address... – War10ck Apr 18 '16 at 19:07
  • War10ck is right, this regex also allows impossible ip adresses. The solution of Wiktor Stribiżew is the correct one. – Reto Apr 18 '16 at 19:29
  • This was just for matching an ip addresses at a first glance. Of course the response wasn't as accurate as needed but worked for the basic needs – lindusilois Apr 18 '16 at 19:31
0

If you are trying to match an address that looks like this 10.123.123.123/1

try this

 10\.\d{1,3}\.\d{1,3}\.\d{1,3}

if you the address is supposed to take up the whole line and leave room for nothing else on the line then:

 ^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$

This will match address what have values beyond 255 for each value if you want to restrict that, it gets a little more complicated.

Rob
  • 2,618
  • 2
  • 22
  • 29
0

Most of the answers don't explain why the regex is not working for the OP.

According to this, it will work for IP addresses, although it's not a specific IP address format.

I've added the m modifier so that the assertions (^ and $) match the start and end of each line (for testing multiple IP addresses).

If you are looking for a specific IP address format then Wiktor Stribiżew's answer is the right one.

Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
  • What do you mean by *none of the answers attempt to explain*? My answer contains a clear explanation what OP regex matches at the beginning, and your MULTILINE modifier addition is not adding any value to the real life scenarios, only to a demo at regex101.com. – Wiktor Stribiżew Apr 18 '16 at 21:02
  • @WiktorStribiżew Well I'm glad you've updated your answer. I can update mine now. – Mr. Meeseeks Apr 18 '16 at 21:20
  • I wondered because my last update was at 19:27:50, and your answer was posted at 19:31:30. Actually, again, multiline mode is most probably not the point here. At best you might guess OP's typo in the regex. – Wiktor Stribiżew Apr 18 '16 at 21:23
  • @WiktorStribiżew maybe my page wasn't showing your answer when I posted :/ but you're right, multiline isn't the point, hence the "*for testing multiple IP addresses*" – Mr. Meeseeks Apr 18 '16 at 21:24