如何使用RegEx提取字符串的IP [英] How to extract the IP of the string using RegEx

查看:91
本文介绍了如何使用RegEx提取字符串的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用RegEx提取下面的字符串的IP?

How to extract the IP of the string below using RegEx?


... sid [1544764] srv [CFT256] remip [10.0.128.31] fwf []...

我尝试了下面的代码,但未返回预期值:

I tried the code below but did not return the expected value:


string pattern = @"remip\ \[.\]";
MatchCollection mc = Regex.Matches(stringToSearch, pattern );



提前致谢。


Thanks in advance.

推荐答案

试试这个:

@"remip \[(\d+\.\d+\.\d+\.\d+)\]"

澄清。 ..你的工作不起作用的原因是因为你只在 [和<$ c $>中匹配 C> 。单个仅匹配单个字符。您可以添加 * (零或更多)或 + (一个或多个)以使其正常工作。另外,用括号括起来:,意味着你可以直接从第二个中提取IP地址 MatchCollection 中的项目。

To clarify... the reason yours doesn't work is because you are only matching . inside the [ and ]. A single . matches only a single character. You could add a * (zero or more) or a + (one or more) to make it work. In addition, surrounding it with parenthesis: ( and ), means you can extract just the IP address directly from the second item in the MatchCollection.

这篇关于如何使用RegEx提取字符串的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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