I searched for an answer but I couldn't find a clear one. Please, bear with me as I'm kind of a noob in regex, and this is my first question too. I'm using Python 3, but will also be needing this for Javascript too.
What I'm trying to do is validate an input by the user. The input is an inequality (spaces removed), and the variables are named by the user and given beforehand. For example, let's say I have this inequality:
x+y+6p<=z+1
The variables x, y, p, z will be given. The problem now if the inequality is like this:
xp+yp+6p<=z+1
The given variables are xp, yp, p, and z. I'm trying to write a regular expression to match any inequality with such a format, given no spaces in the inequality. I cannot figure out how to check for alternative strings. For example I wrote the following expression:
^([\+\-]?[0-9]*([xpypz]|[0-9]+))+[<>]=([\+\-]?[0-9]*([xpypz]|[0-9]+))+$
I know this is completely wrong and that's not how the parentheses are used, but I don't have a feasible expression and I wanted to show you what I want to achieve. Now I need to know three things (at least, I hope) to fix it:
- How to check specifically for xp, and yp as they are literally instead of all characters in the set xypz?
- How to make 0-9 after xpypz work as [0-9]+? Meaning that any number can occur instead of a variable?
- How can I repeat make the whole group repeated
I'm trying to write this expression to check if the user is adding undeclared variables. I believe this can be done differently without using regex, but it would be nice to do it in a single line. Can you please help me figure out those three point? Thanks.