I have a polynomial and I want to separate it into terms. that is, having the polynomial: "3x^2+x(2-4)-1"
I want to separate it into: 3x^2
, +x(2-4)
and -1
. For that I have to separate it by the +
and -
that it finds except those that are inside the parentheses.
I tried to use the following statement: re.split("[\+|\-][^[\(.*[\+|\-].*\)]]", poly)
to separate it but it didn't work.
My question is: can it be done with regex or is there a python module that can do this.
We can create a function to separate the terms of a polynomial.
The idea is to go through the polynomial and read all the characters one by one (sequentially).
And what the function does is depends on which character we are reading at the moment.
We need three key variables:
1. currentTerm ( a string )
When we pass through polynomial, we store each character in CurrentTerm until an integer term is formed and store it in the variable
terminos
. And then, we reset CurrentTerm to save new characters and to find another whole term.2. terms ( a list of string )
This is the result of the function, saving all the terms in the polynomial. The content is like this:
['3x^2','+x(2-4)','-1']
3. numberParenthesis ( an integer )
This variable checks if we are inside a pair of parentheses. It works like this: if a is found
(
, the value is added by 1. And if it is found)
, the value is subtracted by 1. So, if parenthesisNumber is equal to 0, it means that we are not inside a pair of parentheses and vice versa.It is useful when we find operators (
+
or-
). If we are out of parentheses, we create a new term. Otherwise, we do not create a new term.The code:
You can test the function with this code:
The result is:
You can use the following regular expression "([+-]?[^-+]+)" . I think the grace is in its simplicity although it can be improved.
It is a regular expression to obtain each of the terms of the polynomial (sequence of characters) whose initial character is an empty string ('') or the character '+' or the character '-' and whose final character group (not included) does not contain neither + nor - . For example '3x^2' or '+3x^2' would fit in your regular expression.
Here you can test it better.
An example with code is as follows: