一段字符串如下:Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.E:\工作\PingServer\PingServer\bin\Debug>ping -n 1 [192.168.0.145]Pinging A.redirect.local [221.231.141.211] with 32 bytes of data:Ping statistics for 221.231.141.211: Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
E:\工作\PingServer\PingServer\bin\Debug>exit我要截取上面的[221.231.141.211],
因为ping -n 1后面有时是[192.168.0.145],有时是navistar.uicp.net,有时又是别的,所以我不能用.Split('[')[3].Split(']')[0]来截取,
必须从后往前截取,取第一个"["开始的,"]"结束的字符串!请大家帮忙想一下!
(C) 版权所有 1985-2001 Microsoft Corp.E:\工作\PingServer\PingServer\bin\Debug>ping -n 1 [192.168.0.145]Pinging A.redirect.local [221.231.141.211] with 32 bytes of data:Ping statistics for 221.231.141.211: Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
E:\工作\PingServer\PingServer\bin\Debug>exit我要截取上面的[221.231.141.211],
因为ping -n 1后面有时是[192.168.0.145],有时是navistar.uicp.net,有时又是别的,所以我不能用.Split('[')[3].Split(']')[0]来截取,
必须从后往前截取,取第一个"["开始的,"]"结束的字符串!请大家帮忙想一下!
解决方案 »
- C#怎么给自己在图片上画的图形上添如长宽高的注释.
- 条形码 编程,求助大侠
- 用C#做监控软件
- tabcontor选择问题
- List 在web端写?
- c#如何process:dexplore.exe
- 麻烦问题。怎样用存储过程实现这个!顶有分
- 高手请进,为什么C#中不能取CPU的数量?
- Navicat for SQL 的查询分析器可以调用,程序MySqlCommand调用失败 这是一个坑吗???
- c#中用linq查询datatable的问题
- C#的Winform程序,sql 分组统计,然后在dataGridView中显示
- 紧急求救!.net中如何让 decimal 类型和 double 类型 相除
Regex rgx = new Regex(@"\[(\d){1,3}.(\d){1,3}.(\d){1,3}.(\d){1,3}\]");
MatchCollection math=rgx.Matches(arr);
foreach (Match match in math)
Console.WriteLine(match.Value);
Regex rgx = new Regex(@"\[(\d){1,3}.(\d){1,3}.(\d){1,3}.(\d){1,3}\]");
color]要匹配[]就这种。如果不匹配[]就把\[和\]去掉
int index = str.IndexOf("Ping statistics for ")+20;
string ip = str.Substring(index,str.IndexOf(":",index)-index);
Regex re = new Regex("(?<=pinging[^[]*)\[(\d){1,3}.(\d){1,3}.(\d){1,3}.(\d){1,3}\]");
MatchCollection mc = re.Matches(input);
foreach(Match m in mc)
{
Console.WriteLine(m.Value);
}
Regex re = new Regex("(?<=pinging[^[]*)\[(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}\]");
MatchCollection mc = re.Matches(input);
foreach(Match m in mc)
{
Console.WriteLine(m.Value);
}
//这回对了吧 哈哈
string[] sArray_B=sArray_A[sArray_A.Length-1].Split('[');
string IPstr=sArray_B[0];C#没有用过,也不知道上面的代码是否有问题。
string[] sArray_B=sArray_A[sArray_A.Length-1].Split(']');
string IPstr=sArray_B[0]; C#没有用过,也不知道上面的代码是否有问题。
int Flag1;
int Flag2;
Flag2 = TestStr.LastIndexOf(']');
if (Flag2 > 0)
{
Flag1 = TestStr.LastIndexOf('[', Flag2);
if (Flag1 > -1)
{
Flag1 = Flag1 + 1;
String result = TestStr.Substring(Flag1, Flag2 - Flag1);
MessageBox.Show(result);
}
}
private static void IPstring()
{
string input = @"kasdfj;jkldaf[111.111.111.111],sadf
sdf[222.222.222.222]"; string pattern = @"\[(\d{3}\.){3}\d{3}\]"; Regex r = new Regex(pattern);
MatchCollection m = r.Matches(input); List<string> listStr = new List<string>();
foreach (Match nextMatch in m)
{
listStr.Add(nextMatch.ToString().Trim());
GroupCollection g = nextMatch.Groups;
}
}
listStr 中结果为[111.111.111.111],[222.222.222.222]
如不想要'[',']',可以用string pattern = @"(\d{1,3}\.){3}\d{1,3}";
private static void IPstring()
{
string input = @"kasdfj;jkldaf[111.111.111.111],sadf
sdf[222.222.222.222] dsfds d
[1.2.2.33]"; string pattern = @"\[(\d{1,3}\.){3}\d{1,3}\]"; Regex r = new Regex(pattern);
MatchCollection m = r.Matches(input); List<string> listStr = new List<string>();
foreach (Match nextMatch in m)
{
listStr.Add(nextMatch.ToString().Trim());
GroupCollection g = nextMatch.Groups;
}
}
listStr 中结果为[111.111.111.111],[222.222.222.222],[1.2.2.33]
这样可以满足你所说的要求,但有一个缺陷是[999.999.999.999]也能满足