Reputation:
I'm trying to write this code, where a program takes the input in the form: (x - / + y)(x / + y)
I've come this far (ignore the prints as it's written in swedish):
user_input = list(input('Skriv ett tal på formen (x +/- y)(x +/- y): '))
operator_list = []
for i in range(len(user_input)):
if user_input[i] == '-' or user_input[i] == '+':
operator_list.append(i)
def method():
if user_input[operator_list[0]] == '-' and user_input[operator_list[1]] == '-':
return('2:a kvadreringsregeln')
elif user_input[operator_list[0]] == '+' and user_input[operator_list[1]] == '+':
return('1:a kvadreringsregeln')
elif user_input[operator_list[0]] != user_input[operator_list[1]]:
return('Konjugatregeln')
print('Ditt tal:', ''.join(user_input), 'är skriven enligt:', method())
However right now the program doesn't require the user to input it with any parenthesis, or in any particular order,
i.e the input can be:
(-- or ( -a)(+
etc.
However I want to user to only be able to input the code in this form:
(x -/+ y)(x -/+ y) or (x-/+y)(x-/+y) or (x -/+y)( x -/+y)
With spaces being of no importance
I was thinking of using the python import regex but I don't know how it works so help would be appreciated :D
Upvotes: 0
Views: 194
Reputation: 579
You can do a Regex with re
package with the match
function :
import re
input = "(x +/-y)(x+/-y )"
pattern = "\(x\+/-y\)\(x\+/-y\)"
if re.match(pattern, input.lstrip()) is not None:
print("your input is on the correct format")
else:
print("your input is not correct")
Note that the \
character escape special characters (like "()" or "+", ect...). Also, the .lstrip()
function removes all white space in your string.
Here, the pattern match x
and y
as characters, but if you want to check them as integer, your pattern would look like this : "\(\d+\+/-\d+\)\(\d+\+/-\d+\)"
.
The \d+
check if the string contains 1 or more digits (between 0 and 9).
For more information about Regex in Python, you can have a look at the w3school website.
EDIT :
If you want to include variable like 43x, 4y... you can do that with the following pattern : "\(\d+\D*\+/-\d+\D*\)\(\d+\D*\+/-\d+\D*\)"
with \D*
zero or more string that DOES NOT contain digits.
Upvotes: 0
Reputation: 1018
Yes, regex is the right idea. You basically want to write a regular expression that will only match the text you consider "good".
To give a very quick overview of regex, if you write r"\d+[A-Z][a-z]*"
that would only match strings that start with one or more digits \d+
, followed by an uppercase letter [A-Z]
, followed by a lowercase letter [a-z]
, and ending with whatever *
.
Your use case sounds complicated, but let me try:
import re
pattern = r'\(\w+[+|-]\w+\)\(\w+[+|-]\w+\)'
def test_match(pattern):
assert re.match(pattern, '(x+y)(x+y)')
assert re.match(pattern, '(x+y)(x-y)')
assert re.match(pattern, '(x-y)(x+y)')
assert re.match(pattern, '(x-y)(x-y)')
assert re.match(pattern, '(a-b)(c-d)')
assert not re.match(pattern, '(x?y)(x-y)')
assert not re.match(pattern, '(x-y(x-y)')
assert not re.match(pattern, '(x-y)(x--y)')
test_match(pattern)
And then you could validate your input by doing
if re.match(pattern, input_text.replace(' ',''):
...
Do you need x
& y
to be the only valid inputs for the variables?
Upvotes: 1