Hey, im trying to make a regular expression to Match a quadratic Polynom of the Form +-(any integer)x^2+-(any integer)x+-(any integer) So far i got import re quadratic_polynom=r'-?\d*(x\^2)[\+-]?(\d*x)?[\+-]?\d*' while True: userinput=input(':') if userinput == 'quit': break elif re.match(quadratic_polynom,userinput): # Code.... But the Problem is, i cant get the regex to dont Match for Example x^2+ With a missing integer Or x
1/22/2017 9:03:10 PMAlexander Bauer
6 AnswersNew Answer
I only know about Perl standard regex and I hope it's similar to yours. /(-?([1-9]\d*)?x\^2)([+-]([1-9]\d*)?x)?([+-]([1-9]\d*))?/ I'll try to explain how it works, -? means we are not sure if there is a minus sign or not. [1-9]\d* checks for one or more numbers. x\^2 checks for x squared but ^ is escaped with a backslash. The rest works the same but make sure you do (...)? because quadratic equations can just be 2x^2, they don't have to be 2x^2 + 3x + 5.
Edited my answer. Yeah you enclose everything with a question mark because that can be absent in a quadratic equation.
Well Thank you for your answer, is the double paranthesis "([+-] (...))?" preventing The regex from matching for Example "x^2+" ? My regex Works good so far, but the m Problem was i couldnt get it to Miss Match expressions like that
I tried it, still doesnt work :/ yours also seem to dont Match x^2 without integers infront of it
Still doesnt work :/ maybe ruby is diffrent in this small Part. My only Problem is, that the regex matches "x^2+" or "x^2+x+", the rest Works fine
Its still not working :/