52

I try to extract the value (IP Address) of the wan_ip with this sourcecode: Whats wrong?! I´m sure that the RegEx pattern is correct.

String input = @"var product_pic_fn=;var firmware_ver='20.02.024';var wan_ip='92.75.120.206';if (parent.location.href != window.location.href)";
Regex ip = new Regex(@"[\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
string[] result = ip.Split(input);

foreach (string bla in result)  
{
  Console.WriteLine(bla);                
}

Console.Read();
John Saunders
  • 160,644
  • 26
  • 247
  • 397
h0scHberT
  • 543
  • 1
  • 4
  • 6
  • 4
    The expression aside, shouldn't you be looking at `Regex.Matches` rather than `Split`? – Ani Feb 03 '11 at 19:38
  • possible duplicate of [Regular expression to match hostname or IP Address?](http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address) – Daniel DiPaolo Feb 03 '11 at 19:40
  • 7
    `999.999.999.999` ? – Andrey Feb 03 '11 at 19:47
  • 1
    @Andrey that's okay, it is the string which could be lately just checked in with `ipaddress.TryParse`, or something. The main here is to get the string which is supposed to be an IP. – Hi-Angel Dec 29 '14 at 07:30
  • Exception. Remove the first [ in the Regex String – Darkgaze Apr 21 '21 at 07:59

13 Answers13

55

The [ shouldn't be at the start of your pattern. Also, you probably want to use Matches(...).

Try:

String input = @"var product_pic_fn=;var firmware_ver='20.02.024';var wan_ip='92.75.120.206';if (parent.location.href != window.location.href)";
Regex ip = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
MatchCollection result = ip.Matches(input);
Console.WriteLine(result[0]); 
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • 1
    @h0scHberT, if @John's suggestion works, I'd go for that. If not, give @SwDevMan81's code a try: it looks safer to first check if there's a match. My (quick) suggestion will probably produce errors if there's no match (I'm a bit of a C# novice...). – Bart Kiers Feb 03 '11 at 19:54
  • It doesn't work for a case if IP is in 192.168.1.15_1 – Jay Mar 12 '14 at 23:03
  • doesn't work for 008.008.008.008 that is not a valid IPAddress but your pattern accept it. – Mohammad Jun 06 '15 at 09:48
  • 4
    I never claimed it would only match valid ip addresses. Such a thing should not be done with regex. – Bart Kiers Jun 06 '15 at 10:05
49

Very old post, you should use the accepted solution, but consider using the right RegEx for an IPV4 adress :

((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]?)

If you want to avoid special caracters after or before you can use :

^((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]?)$
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
17

Try this:

 Match match = Regex.Match(input, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
 if (match.Success)
 {
     Console.WriteLine(match.Value);
 }
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
16

If you just want check correct IP use IPAddress.TryParse

using System.Net;

bool isIP(string host)
{
    IPAddress ip;
    return IPAddress.TryParse(host, out ip);
}
Geograph
  • 2,274
  • 23
  • 23
6

I know this post isn't new, but, I've tried several of the proposed solutions and none of them work quite as well as one I found thanks to a link provided by Justin Jones. They have quite a few for IP Address but this is the top of the list and using LinqPad (I LOVE LinqPad) most tests I've thrown at it work extremely well. I recommend utilizing this one rather than any of the previous provided expressions:

^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$

Give that a shot in LinqPad with the following:

// \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b  355.168.0.1 = 355.168.0.1 (Not Correct)
// ((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]?) 355.168.0.1 = 55.168.0.1 (Not correct)
// \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}  355.168.0.1 = 355.168.0.1 (Not Correct)
// ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$  355.168.0.1 = 355.168.0.1 (Not Correct)
// ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$  355.168.0.1 = 355.168.0.1 (Not Correct)
// ^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$  355.168.0.1 = No Match (Correct)

Match match = Regex.Match("355.168.0.1", @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
if (match.Success) {
    Console.WriteLine(match.Value);
}
else {
    Console.WriteLine("No match.");
}

With the new RegEx this is not valid which is correct: 355.168.0.1 = No Match which is correct as noted in the comments.

I welcome any tweaks to this as I'm working on a tool that is making use of the expression and am always looking for better ways of doing this.

UPDATE: I've created a .NET Fiddle project to provide a working example of this expression along with a list of IP Addresses that test various values. Feel free to tinker with it and try various values to exercise this expression and provide any input if you find a case where the expression fails. https://dotnetfiddle.net/JoBXdI

UPDATE 2: Better yet refer to this post: Another related question.

Thanks and I hope this helps!

Erick Brown
  • 618
  • 11
  • 22
  • This one returns false for "0.254.255.0" – dont_trust_me Dec 27 '17 at 13:39
  • @dont_trust_me Thanks for pointing that out. It seems to work correctly in my .NET Fiddle project linked above (I added your example to the fiddle project). "0.254.255.0 Does not match. (Considered Invalid)" - How are you getting valid? – Erick Brown Apr 17 '18 at 16:37
3
Regex.IsMatch(input, @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$")
Manish
  • 517
  • 1
  • 3
  • 19
  • 10
    Why would you answer an already answered question more than a year after it was asked with a solution that doesn't work? The question was about extracting a substring - your solution only matches an exact string, and adds absolutely _nothing_ to previous answers. – Simon MᶜKenzie Jul 05 '12 at 03:28
3

Avoid using /b - it allows characters before or after the IP
For example ...198.192.168.12... was valid.

Use ^ and $ instead if you can split the input into chunks that would isolate the IP address.

     Regex regexIP = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$");

     if (regexIP.Match(textBoxIP.Text).Success){
         String myIP = textBoxIP.Text;
     }

Note above will not validate the digits, as pointed out 172.316.254.1 was true. This only checks correct formatting.


UPDATE: To validate FORMATTING and VALUES you could use

     Regex regexIP = new Regex(@"^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$");

     if (regexIP.Match(textBoxIP.Text).Success){
         String myIP = textBoxIP.Text;
     }

(note using ([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]) for each numeric value)
Credit: https://stackoverflow.com/a/10682785/4480932

Cordell
  • 1,901
  • 1
  • 13
  • 12
1

I took this pattern from UrlAttribute.cs. at DataAnnotations namespace. As you may see, I took just a piece of the original pattern from source.

Regex.IsMatch(input, @"^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-
9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-
9]\d|1\d\d|2[0-4]\d|25[0-5])$");
Denis
  • 833
  • 3
  • 12
  • 22
  • Please explain it, so that it provides value to those who would actually search for this question. To those who understand your code without explanation, the solution would already be obvious. – jpaugh Aug 10 '17 at 17:51
  • This works correctly per testing by using my .NET Fiddle project. Not 100% certain what the big differences are between this and my expression. – Erick Brown Oct 12 '17 at 12:23
0

Regex(@"\A\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\z") try with this

Adit Kothari
  • 421
  • 3
  • 16
  • This does not work correctly, see my link to the .NET fiddle above, I've added this expression to the list of tests. – Erick Brown Oct 12 '17 at 12:18
0
(\d{1,3}\.){3}(\d{1,3})[^\d\.] 

Check this. It should work perfectly

  • Sorry, this does not work. Check my .NET Fiddle project linked in my answer - I added this expression to the list. You can comment out mine, un-comment yours and notice it doesn't work as expected. – Erick Brown Oct 12 '17 at 12:26
0

I think you need to get rid of the [ - is that a stray character or what?

Regex(@"[\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b")

Cade Roux
  • 88,164
  • 40
  • 182
  • 265
0

Another variant, depending on how you want to treat padding (e.g. a.b.c.00 is considered invalid format):

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

doveryai
  • 61
  • 5
-1

In Python:

>>> ip_regex = r'^{0}\.{0}\.{0}\.{0}$'.format(r'(25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)')
>>> match(ip_regex, '10.11.12.13')
<re.Match object; span=(0, 11), match='10.11.12.13'>
>>> _.groups()
('10', '11', '12', '13')
>>>
李鸿章
  • 343
  • 1
  • 2
  • 10