我试着拆分字符串,比如:
((12-5=10)&&(5-4>6))
至:
我尝试使用Regex,但是这个说教的结果与一对括号有关,我无法找到解决问题的方法。
发布于 2013-01-18 07:21:10
我想你可能需要这样的东西。至于插入部分,我知道它有点蹩脚
string test = "((12-5=10)&&(5-4>6))";
string[] Arr= test.Split(new string[{"(",")"},StringSplitOptions.RemoveEmptyEntries);
List<string> newArr = new List<string>();
int h=0;
foreach (string s in Arr)
{
if (s != "&&")
newArr.Add( s.Replace(s, "(" + s + ")"));
else
newArr.Add(s);
h++;
}
newArr.Insert(0, "(");
newArr.Insert(newArr.Count , ")");
发布于 2013-01-17 18:18:01
如果您想手动执行,而不是尝试使用regex,想象一下一次只解析一个字符串,然后在匹配大括号时递归地拆分。
伪码:
initialize depth and start to 0
for each character
if it is ( increase depth
if it is )
decrease depth
if depth is 0
parse the substring from start to current character
set start to current character
如果不需要手动操作,那么使用一些外部包。
https://stackoverflow.com/questions/14385455
复制相似问题