I'm wondering if you're offering a bounty.
Simple recursion in regex is a gosub with a pass/fail return that happens to be stackable.
Below is a Perl routine that passes Perl's own parsing algol for the simple operators and simple syntax rules you specified. It's done in a single regex because your needs are extremely simple.
It looks fancy but resolves to the simple balanced text of '()'. It looks like Dot Net
can do this. It should be real easy to just do the substitution's (ie; (?&var) ), do
the balanced grouping Dot Net requires... instant validation.
I'm posting this because, nesting is not the problem. The problem is that as simple as
parsing seems, the devil is in the details.
^
(?:
^ (?&sign)? (?&number)
|
(?&operator)
(?<! ^ (?:\/|\*) )
(?<! ^ [*]{2} )
(?&sign)? (?&number)
|
(?: (?&operator)
(?<! ^ (?:\/|\*) )
(?<! ^ [*]{2} )
(?<! [(] (?:\/|\*) )
(?<! [(] [*]{2} )
(?&sign)?
|
(?<= [(] )
|
^ (?&sign)?
)
(?<term>
\(
(?:
(?> (?&sign)?
(?&number)
(?: (?&operator) (?&sign)? (?&number) )*
)
|
(?>
(?: (?<= [(] ) | (?&operator) )
(?<! [(] (?:\/|\*) )
(?<! [(] [*]{2} )
(?&sign)?
(?&term)
)
)*
\)
(?! [(] )
(?> (?&operator) (?&sign)? (?&number) )*
)
)*
$
(?(DEFINE)
(?<number> \d+(?:\.\d+)? )
(?<sign> [+-] )
(?<operator>
(?: [*]{2}
| [\/*]
| (?<pm>[-+]) (?! \k<pm>) )
)
)
Output
passed ''
passed '(6**-2**3)'
passed '6-+2'
passed '-(-(8*((2)/3)))'
passed '-((8*((2)/3)))'
passed '-((8*((2**4)/3)))'
passed '-((8*((2**4)/3)))**((-1)*(8*((2**4/99)/3)))'
passed '-((8*((2**4)/3)))**((-1)*(8*((2**-4/99)/3)))'
passed '-((8*((2**4)/3)))**-((-1)*(8*((2**-4/99)/3)))'
passed '((8*((2)/3)))'
passed '((8*((2)/3)))'
passed '+((8*((-2)/-3)))'
passed '8-6*2'
passed '-8-6*2'
passed '((8*((2-(8*(8+6)/2))/3))-7*2/234)+8/2*1'
passed '-(8*(8+6)/2)'
passed '(9*9/9)'
passed '(9*(9)/9*(9*(9)/9)*1)'
passed '(9*(9)/9*(9*(9)/9))*(9*(9)/9*(9*(9)/9))'
failed '(6--2)'
failed '-(/(8*((2)/3)))'
failed '-((8*((2(6))/3)))'
failed '+((8*((+2)/--3)))'
failed '+((8*((+2)/--3+)))'
failed '+((8*((*2)/--3)))'
failed '+((8*((*2)/-3)))'
failed '-((8*((-2)/+-3)))'
failed '+8/2(1)'
failed '-(8)*(8/('
failed '-(8)*(8/()'
failed '*(9*9/9)'
failed '*(9*(9)/9*(9*(9)/9))*(9*(9)/9*(9*(9)/9))'
failed '/(9*(9)/9*(9*(9)/9))*(9*(9)/9*(9*(9)/9))'