ztoolson
ztoolson

Reputation: 55

C# Split up string representation of a formula

I am new to using regular expression and am having problems parsing a string. I am working on parsing a Formula given as a string using regular expression.

Currently my regular expression is:

string[] formula_substrings = Regex.Split(formula, "(\\()|(\\))|(-)|(\\+)|(\\*)|(/)|(\\s+)")

One of the test cases that this regular expression is unable to handle is when the formula has exponents.

Example:

Formula = "1e-2 + 5.0"

My desired output is

["1e-2", "+", "5.0"] 

but I am getting

["1e", "-", "2", "+", "5.0"].

I tried changing the regular expression to make sure the character in front of the minus sign ins't an e:

string[] formula_substrings = Regex.Split(formula, "(\\()|(\\))|((^e)-)|(\\+)|(\\*)|(/)|(\\s+)")

And this is causing problems when the expression is

Formula = "5+5-2"

It will give the output:

["5", "+", "5-2"] 

When my desired output is

["5", "+", "5", "-", "2"]

Thanks in advance for the help!

Upvotes: 0

Views: 844

Answers (1)

Anirudha
Anirudha

Reputation: 32797

You can use lookbehind..

Regex.Split(formula, @"(\(|\)|(?<!e|E)-|(?<!e|E)\+|\*|/|\s+)")
                              --------  --------

Upvotes: 3

Related Questions