tags:

views:

299

answers:

7

Say you have an IP address: 74.125.45.100 so its A.B.C.D

Is there a way to use RegEx to get A,B,C separately?

+3  A: 
/(\d+)\.(\d+)\.(\d+)\.(\d+)/
David Dorward
From the above your regex parser should return parameters 1, 2, 3 and 4 which can then be processed separately.
Lazarus
Note this won't validate an IP address (it'd accept 111111111111.1.000000000000.1234567890) but you didn't ask for that so it's probably okay!
butterchicken
Each bracketed section '()' returns a value from the regex
Lazarus
/([012]\d{,2})\.([012]\d{,2})\.([012]\d{,2})\.([012]\d{,2})/ ?
Lazarus
The question only asked to extract the values, not to validate the syntax. :)
David Dorward
+5  A: 

([0-9]+).([0-9]+).([0-9]+).([0-9]+)

...should do it. It's no validating regex though, allows numbers beyond 255 for each part.

Here's a crazy validating one:

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

Credit to last regex goes to RegexBuddy makers.

Skurmedel
So maybe ([0-9][0-9][0-9]).([0-9][0-9][0-9]).([0-9][0-9][0-9]).([0-9][0-9][0-9])\ :)
amischiefr
@amischiefr, that would accept 000-999, while the one Skurmedel provided only accepts 000-255, the later being a correctly formatted ip address.
Simon Svensson
+1 for the regex that validates ip addr
Rashmi Pandit
+6  A: 

Something very simple yet ugly would work.. giving you four groups one for each octet.

(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})
Quintin Robinson
+9  A: 

If it is just to extract the numbers from the IP and not to validate the IP address then you could just do:

[0-9]

However, I think a simple String.Split(".") would be an easier option.

James
+1 for suggesting String.Split, because using a regex for this is silly, unless you're trying to *find* an ip address in a longer string.
Brian
+1 for String.Split, it sounds like a lot healthier solution that building a state machine (which regex is).
Simon Svensson
It's definately easier, the only caveat would be if the IPs appear at arbitrary positions in a text body.
Skurmedel
+1  A: 

First port of call for regex... RegEx Library

Lazarus
Hmmm. This does have a few solutions that should work.
Brian
A: 

While others have pointed out various good regexps; May I ask why you absolutely must use regular expressions for that? It will be slow and error-prone. Most platforms do have integrated IP address functionality, or provide a way to call to inet_aton.

phihag
+1  A: 

In case someone needs a validating RegEx for (all possible) IPv4 addresses:

([^\d.]|^)([01]{0,1}\d{1,2}|2[0-5][0-5])[.]([01]{0,1}\d{1,2}|2[0-5][0-5])[.]([01]{0,1}\d{1,2}|2[0-5][0-5])[.]([01]{0,1}\d{1,2}|2[0-5][0-5])([^\d]|$)

The IP is contained in 2nd, 3rd and 4th parameters. 1st and last are not used. Those are necessary otherwise a wrong IP like:

999.1.2.3

would be catched as "99.1.2.3". I am not sure if you want to allow IP ending with a dot, e.g.

1.2.3.4.

If not, change the last part to ([^\d.]|$). I do not allow any dots in front of it though.

I still think this RegEx is a messed monster :) and a better solution would be to validate by hand using a function.

Howdy