A regular expression isn't exactly the best tool for the job. You can achieve what you want with them, but you'll have to jump through hoops.
The first one being nested constructs like 45+((10 + 20)*2)<=50
, so let's start working on that first, as \(.*\)
won't do you any good. It's eager and unaware of nested constructs.
Here's a better pattern for parentheses only:
(?>
(?<p>\()
|(?<-p>\))
|(?(p)[^()])
)+
(?(p)(?!))
Yes, that's what it takes. Read about balancing groups for an in-depth explanation of this.
Numerical terms would be matched by \d+
or [0-9]+
(for ASCII only digits in .NET), not by \w+
.
As for your question:
A numerical or parenthetical term followed by an operational term, and that whole thing repeated any number of times, ending in a numerical or parenthetical term
You're trying to do it wrong. While you could do just that with PCRE regexes, it'll be much harder in .NET.
You can use regexes for lexing (aka tokenizing). But then use application code to make sense of the tokens the regex returns you. Don't use regex for semantics, you won't end up with pretty code.
Perhaps you should use an existing math parsing library, such as NCalc.
Or you may need to go with a custom solution and build your own parser...