Use regex pattern:
/((?:(?=[^()]*\d)\d*(?:\.\d*)?)|(?:\((?:(?:[^()]+)|(?:[^()]*\([^()]*\)[^()]*)+)\)))\^((?:(?=[^()]*\d)\-?\d*(?:\.\d*)?)|(?:\((?:(?:[^()]+)|(?:[^()]*\([^()]*\)[^()]*)+)\)))/
Explanation:
[Step 1] :: Regex patter contains two almost same sub-patterns linked with ^
sign between
((?:(?=[^()]*\d)\d*(?:\.\d*)?)|(?:\((?:(?:[^()]+)|(?:[^()]*\([^()]*\)[^()]*)+)\)))
\^
((?:(?=[^()]*\d)\-?\d*(?:\.\d*)?)|(?:\((?:(?:[^()]+)|(?:[^()]*\([^()]*\)[^()]*)+)\)))
The only difference is that second one (behind ^
) allows negative number as a simple parameter (\-?
)
[Step 2] :: Sub-pattern from Step 1 has two alternatives:
(?:(?=[^()]*\d)\-?\d*(?:\.\d*)?)
|
(?:\((?:(?:[^()]+)|(?:[^()]*\([^()]*\)[^()]*)+)\))
[Step 3] :: First alternative is a number - for example: 1234
or 12.34
or 1234.
or .1234
(?=[^()]*\d)
\-?\d*
(?:\.\d*)?
[Step 4] :: Second alternative is a nested parenthesis formula
\(
(?:(?:[^()]+)|(?:[^()]*\([^()]*\)[^()]*)+)
\)
[Step 5] :: which might be simple or complex (with other parenthesis inside)
(?:[^()]+)
|
(?:[^()]*\([^()]*\)[^()]*)+
[Step 6] :: and if is complex and have some other parenthesis inside, we ensure there are nested
[^()]*
\(
[^()]*
\)
[^()]*