检查是否一个字符串包含一个IP地址 [英] check to see if a string contains an ip address

查看:351
本文介绍了检查是否一个字符串包含一个IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经加载到流读取的文件。该文件包含散落的IP地址。前后各有IP地址有\如果这有助于。此外,第一\在每一行永远是在IP地址之前,有这第一个之前没有其他的\。



我已经知道我应该通过每一行使用whil​​e循环周期,但我不知道该过程的其余部分:其中;



例如:



由Stormix.de\93.190.64.150\7777\False的powerd



Cupserver\85.236.100.100\8178\False



欧元Server\217.163.26.20\7778\False



在第一个例子中我需要在第二个例子中93.190.64.150
我需要85.236.100.100
中的第三个例子,我会需要217.163.26.20



我真的解析/拼接/切割挣扎:■



在此先感谢



* 的我需要保持IP字符串中的布尔回报是不够的什么我想做的事情。


解决方案

 使用System.Text.RegularExpressions; 

...

VAR sourceString =把你的字符串在这里;
VAR匹配= Regex.Match(sourceString,@\b(\d {1,3} \.\d {1,3} \.\d {1,3} \\ \\.\d {1,3})\b);
如果(matches.Success)Console.WriteLine(match.Captures [0]);

这将匹配任何IP地址,也 999.999.999.999 。如果你需要更多的正确性,详情请参阅这里: http://www.regular-expressions.info/examples.html



该网站有很多伟大的信息的正则表达式,这是文本模式匹配最流行的编程语言中使用的领域特定语言。其实,我认为该网站是由精通正则表达式的作者放在一起。



更新



我修改了上面的代码捕获IP地址,你的要求(由围绕着IP地址模式添加括号)。现在,我们检查以确保有使用成功属性的匹配,然后就可以使用获取IP地址捕获[0] (因为我们只有一个捕获组,我们知道使用第一个指数,0)。


I have a file loaded into a stream reader. The file contains ip addresses scattered about. Before and after each IP addresses there is "\" if this helps. Also, the first "\" on each line ALWAYS comes before the ip address, there are no other "\" before this first one.

I already know that I should use a while loop to cycle through each line, but I dont know the rest of the procedure :<

For example:

Powerd by Stormix.de\93.190.64.150\7777\False

Cupserver\85.236.100.100\8178\False

Euro Server\217.163.26.20\7778\False

in the first example i would need "93.190.64.150" in the second example i would need "85.236.100.100" in the third example i would need "217.163.26.20"

I really struggle with parsing/splicing/dicing :s

thanks in advance

* I require to keep the IP in a string a bool return is not sufficient for what i want to do.

解决方案

using System.Text.RegularExpressions;

…

var sourceString = "put your string here";
var match = Regex.Match(sourceString, @"\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b");
if(matches.Success) Console.WriteLine(match.Captures[0]);

This will match any IP address, but also 999.999.999.999. If you need more exactness, see details here: http://www.regular-expressions.info/examples.html

The site has lots of great info an regular expressions, which is a domain-specific language used within most popular programming languages for text pattern matching. Actually, I think the site was put together by the author of Mastering Regular Expressions.

update

I modified the code above to capture the IP address, as you requested (by adding parentheses around the IP address pattern). Now we check to make sure there was a match using the Success property, and then you can get the IP address using Captures[0] (because we only have one capture group, we know to use the first index, 0).

这篇关于检查是否一个字符串包含一个IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆