Reputation: 13
i try to split string like:
((12-5=10)&&(5-4>6))
to:
i try to use Regex but the results of the serach relate to one pair of brackets, and i can't find a solution to my problem.
Upvotes: 1
Views: 1070
Reputation: 438
Guess you could use something like this. As for the the Insert part, I know it's kinda lame :/
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 , ")");
Upvotes: 1
Reputation: 2894
If you would like to do it manually, instead of trying to use regex, imagine parsing through the string one character at a time, and recursively splitting at matching braces.
Pseudocode:
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
If there is no need to do it manually, then use some external package.
Upvotes: 1