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 p
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
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!
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]);
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")
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])$");
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);
}