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 :

  1. +x^2
  2. +3x
  3. -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

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -