c++ - Detect terms in an expression using regex -
i want detect terms in polynomial expression
x^2+3x-2
i want string seperated sub-strings @ '+' , '-'. in other words, want have terms seperately. simplify things, add '+' or '-' @ start of string if not present simplify things.
+x^2+3x-2
now, need regex can detect terms seperately. need keep in mind there can many terms possible.
i did try
[+-]?(.+?)
but since new regex, don't think within capability make work.
i need help. please note in case, desired results :
- +x^2
- +3x
- -2
thanks in advance!
a regular expression can limited job state, joseph's answer shows.
but if want handle more complicated expressions regex wont cut it. consider
x^(-2)+3x-2
or unexpanded expression such as
x^2+3(x-2)-2
these require parser: see regular expression vs. string parsing
Comments
Post a Comment