0

Regex problem

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

22nd Jan 2017, 9:03 PM
Alexander Bauer
Alexander Bauer - avatar
6 Respuestas
+ 3
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.
23rd Jan 2017, 1:52 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
Edited my answer. Yeah you enclose everything with a question mark because that can be absent in a quadratic equation.
23rd Jan 2017, 8:11 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
0
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
23rd Jan 2017, 7:30 AM
Alexander Bauer
Alexander Bauer - avatar
0
I tried it, still doesnt work :/ yours also seem to dont Match x^2 without integers infront of it
23rd Jan 2017, 7:55 AM
Alexander Bauer
Alexander Bauer - avatar
0
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
23rd Jan 2017, 8:15 AM
Alexander Bauer
Alexander Bauer - avatar
0
Its still not working :/
23rd Jan 2017, 12:42 PM
Alexander Bauer
Alexander Bauer - avatar