正则表达式的,请大家来聊聊
如下:
上海[1,3],北京[ 2,4,5],山东
就上次用一个字符中,要找到的是
[1,3]
[ 2,4,5]
这两个,条件就是 有[]而[]里又要含有数字的,(只要是数字就可以了,不理是多少)
谢谢
------解决方案--------------------string str = @ "上海[1,3],北京[ 2,4,5],山东 ";
string pattern = @ "\[\s*\d(?:,\s*\d)*\] ";
Regex regex = new Regex(pattern);
MatchCollection mc = regex.Matches(str);
foreach (Match m in mc)
Console.WriteLine(m.Value);
------解决方案--------------------试下
string yourStr = richTextBox1.Text;
MatchCollection mc = Regex.Matches(yourStr, @ "\[(\s*\d,?)*?\] ");
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n "; //提取内容
}
------解决方案--------------------yufenfeila没有考虑[ 13 , 4 , 5 ]和[11,3]的情况
string str = @ "上海[11,3],北京[ 2,4,5],山东[1],天津[ 13 , 4 , 5 ] [] ";
string pattern = @ "\[\s*\d(\s*,\s*\d\s*)*\] ";
Regex regex = new Regex(pattern);
MatchCollection mc = regex.Matches(str);
foreach (Match m in mc)
Console.WriteLine(m.Value);
------解决方案--------------------确实少考虑了一些情况,严谨点的
string yourStr = richTextBox1.Text;
MatchCollection mc = Regex.Matches(yourStr, @ "\[(\s*\d+\s*,?)+?\] ");
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n "; //提取内容
}