2011-02-03 157 views
28

我尝试使用此源代码提取wan_ip的值(IP地址): 什么错误?我确信RegEx模式是正确的。正则表达式的IP地址

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(); 
+4

除了表达式,你不应该看`Regex.Matches`而不是`Split`吗? – Ani 2011-02-03 19:38:46

+0

[正则表达式匹配主机名或IP地址?]的可能重复(http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address) – 2011-02-03 19:40:56

+6

`999.999.999.999` ? – Andrey 2011-02-03 19:47:04

回答

32

[不应该在您的模式的开始。另外,您可能想要使用Matches(...)

尝试:

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]); 
+1

@ h0scHberT,如果@约翰的建议有效,我会为此而努力。如果没有,给@ SwDevMan81的代码尝试一下:看起来比较安全,首先检查是否有匹配。如果没有匹配,我的(快速)建议可能会产生错误(我是一个C#新手......)。 – 2011-02-03 19:54:00

+0

谢谢!有用! – h0scHberT 2011-02-03 20:10:24

+0

如果IP位于192.168.1.15_1 – Jay 2014-03-12 23:03:29

0

我认为你需要摆脱[ - 是一个流浪的字符或什么?

正则表达式(@ “[ \ B \ d {1,3}。\ d {1,3}。\ d {1,3}。\ d {1,3} \ B”)

0

您似乎在您的正则表达式模式的开头有一个无关的[

13

试试这个:

Match match = Regex.Match(input, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"); 
if (match.Success) 
{ 
    Console.WriteLine(match.Value); 
} 
5

如果你发现自己寻找一个特定的正则表达式,http://regexlib.com/有很多正则表达式的在库格式。

3
Regex.IsMatch(input, @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$") 
24

很老的文章,你应该使用接受的解决方案,但考虑到使用正确的正则表达式的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]?) 

如果你想避免特殊caracters或beforeyou可以使用后:

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

避免使用/b - 它允许字符之前或IP
例如...198.192.168.12...是后有效。

改为使用^$,而不是将输入拆分成可隔离IP地址的块。上述

 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; 
    } 

注意不会验证数字时,指出172 .254.1是真实的。这只会检查正确的格式。


UPDATE:为了验证格式和值,您可以使用

 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; 
    } 

(使用([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])每个数值注)
贷:https://stackoverflow.com/a/10682785/4480932

2

我知道这个帖子ISN”新的,但是,我已经尝试了几种建议的解决方案,并且他们没有一个能够像我在Justin Jones提供的链接中找到的那样工作得很好。他们有很多IP地址,但这是列表中的顶部,并且使用LinqPad(我爱LinqPad),大部分测试都是非常好的。我建议利用这一个,而不是任何先前提供的表达式:

^(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])$ 

给在LinqPad一个镜头有以下:

// \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."); 
} 

有了新的正则表达式,这是无效的这是正确的: 355.168.0.1 =不匹配,正如注释中所述。

我欢迎任何调整,因为我正在使用表达式的工具,并一直在寻找更好的方法来做到这一点。

UPDATE:我创建了一个.NET Fiddle项目,提供此表达式的工作示例以及测试各种值的IP地址列表。随意修改它,尝试各种值来运用这个表达式,并提供任何输入,如果你发现表达式失败的情况。 https://dotnetfiddle.net/JoBXdI

更新2:更重要的是指这个帖子:Another related question.

谢谢,我希望这有助于!

0

正则表达式(@“\ A \ d {1,3}。\ d {1,3}。\ d {1,3}。\ d {1,3} \ z”)试一试

0

我从UrlAttribute.cs采取了这种模式。在DataAnnotations命名空间。正如你所看到的,我从源头上拿出了一块原始图案。

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])$"); 
0
(\d{1,3}\.){3}(\d{1,3})[^\d\.] 

检查。它应该很好地工作