求一正则
现有类似这样的数据:12
12(aaa)
(aaa)12(bbb)
最后想得到的数据是:
如果没有括号 则返回不变
如果有括号 则返回最后一个( 前边的内容
如上边三条 我想得到的结果是:
12
12
(aaa)12
--------------------编程问答-------------------- try...
string[] test = new string[] {"12", "12(aaa)", "(aaa)12(bbb)" };--------------------编程问答--------------------
Regex reg = new Regex(@"\([^()]*\)[^()]*$");
foreach (string s in test)
{
richTextBox2.Text += s.PadRight(15, ' ') + "替换后: " + reg.Replace(s, "") + "\n";
}
string[] test = new string[] {"12", "12(aaa)", "(aaa)12(bbb)" };
Regex reg = new Regex(@"(?im)^(.+?)(\([^()]*\))?\s*$");
foreach (string item in test)
{
Console.WriteLine(reg.Match(item).Groups[1].Value);
}
/*输出
12
12
(aaa)12
*/
补充:.NET技术 , C#