Search code examples
c#regexregular-language

c# split arithmetic expression to string[]


I have a simple arithmetic expression containing integers, parentheses, and +-*/. Like this:

((1+33)()(4+(3-5)))

What I need to do is split this expression to the string[] like this:

{(, (, 1, +, 33, ), (, ), (, 4, +, (, 3, -, 5, ), ), )}

I am trying to do it with Regex class

public string[] SplitString(string str)
{
     return Regex.Split(str, "(?<=[()-+*/])|(?=[()-+*/])");
}

But output data is not quite right:

{(, (, 1, +, 33, ), (, ), (, 4, +, (, **3-5**, ), ), )}


Solution

  • If you want to match a dash, it needs to be in the first or the last position in a character class in regex:

    (?<=[()+*/-])|(?=[()+*/-])
    //        ^            ^
    

    Otherwise, it is interpreted as a character range - in your case, from ) to *.

    Demo.