Regex.Split在方括号中选择 [英] Regex.Split select in square brackets
本文介绍了Regex.Split在方括号中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我想要如何拆分此字符串
Hi i want how to split this string
string s = "[1] - blah blah [2] - [bbbbbb]";
string[] words = Regex.Split(s, "??????");
在Regex.Split中如何使用?
i想要这个输出
1
2
bbbbbb
in Regex.Split how to use ?
i want this output
1
2
bbbbbb
推荐答案
我确信具有更多正则表达经验的人可以做得更好。也就是说,试试这个...
I'm sure somebody with more Regular Expression experience could do better. That said, try this...
string s = "[1] - blah blah [2] - [bbbbbb]";
System.Text.RegularExpressions.MatchCollection matches = Regex.Matches(s, @"\[(\w*)\]");
foreach (System.Text.RegularExpressions.Match match in matches)
{
// the item at index 1 is your value without the brackets, the value at index 0 is the value including the brackets wrapping it.
MessageBox.Show(match.Groups[1].Value);
}
Hi
您可以使用匹配并使用foreach循环获取值
希望这会对你有所帮助。
Hi Instead of split you can use Matches and take the values using foreach loop
Hope this helps you.
string pattern = @"\[(.*?)\]";
string str = "[1] - blah blah [2] - [bbbbbb]";
string matches = Regex.Matches(str, pattern);
string value = string.Empty;
foreach (Match m in matches)
{
value += m.Groups[1].ToString()+",";
}
这篇关于Regex.Split在方括号中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文