"[111]asdfasdfasdf[2222]asdfasdf[bbbb]82347[cccc]" 我如何得到[]间的字符? 可以是数组 111,2222,bbbb,cccc 用.net正则如何实现,谢谢.
解决方案 »
- DataGridView导出Excel问题
- C#如何模拟登陆?
- C#中如何实现清除已画的图形
- 关于WebBrowser捕获单击事件遇到.wmv结尾无法响应的问题(急)
- 关于c#调用c#DLL问题
- 调用office com 引起的奇怪问题!
- 请教各位前辈,你们是怎么入的XML?请给我推荐几本最适合入门的XML方面的书籍把!
- C#中new double[5]与new double[5,1]的区别。请各位关注。。。
- 用c#获得网页时,如果有中文就会是乱码,怎么办呢?
- 专家们请帮忙找个东西(关于.Net Connected Logo )
- 关于在C#中,文本框里的内容为空时,系统SQL 空值的问题???
- 为何这段代码不能向界面中添加
string str=@"sssssssssssssssssssssss[POINT=300]内容1[/POINT]ssssssssssssss";
string pattren=@"(?<=[POINT=).*?(?=].*?[/POINT])";
point=int.Parse(Regex.Match(str,pattren).Value);
Response.Write(point);
看这个例子
MatchCollection matchCollection = Regex.Matches(str, pattern);
foreach(Match match in matchCollection)
{
string matchedResult = match.Groups["Info"].Value;
}
str="[111]asdfasdfasdf[2222]asdfasdf[bbbb]82347[cccc]"
用string a="[111]asdfasdfasdf[2222]asdfasdf[bbbb]82347[cccc]" ;
a.SubString(1,3)
第一个参数是“从第几个字符开始截取”,第二个是“截取几位”
这样就可以截取 111 了灰常笨,呵呵
不好意思,pattern多了个},这样:string pattern = "[(? <Info>.*)]";
MatchCollection matchCollection = Regex.Matches(str, pattern);
foreach(Match match in matchCollection)
{
string matchedResult = match.Groups["Info"].Value;
}
//Try it.
String str = @"[111]asdfasdfasdf[2222]asdfasdf[bbbb]82347[cccc]"; Response.Write(String.Format("Source : {0}<br />", str)); str = str.Substring(1, str.Length - 2);
Response.Write(String.Format("Temp : {0}<br />",str)); String[] arrResult = Regex.Split(str, @"\][^\[\]]+\["); for (int i = 0; i < arrResult.Length; i++)
{
Response.Write(String.Format("{0} : {1}<br />",i,arrResult[i]));
}
/*
//Asp.net
Source : [111]asdfasdfasdf[2222]asdfasdf[bbbb]82347[cccc]
Temp : 111]asdfasdfasdf[2222]asdfasdf[bbbb]82347[cccc
0 : 111
1 : 2222
2 : bbbb
3 : cccc
*/
string pattern = "\\[\\w+\\]";
MatchCollection matchCollection = Regex.Matches(str_Source, pattern);
foreach (Match match in matchCollection) {
MessageBox.Show(match.Value);
} 这种方式可以,但是出来的有 [2222] [ssss]如何出来的时候不出[]符号.
string str_Source = "[ssss]asdfas[b_b]dfasdf[2222]asdfasdf[5555]82347[3333]";
string pattern = "\\[\\w+\\]";
MatchCollection matchCollection = Regex.Matches(str_Source, pattern);
foreach (Match match in matchCollection) {
MessageBox.Show(match.Value.ToString().Replace("[", "").Replace("]", ""));
}
}