I am trying to get my head around the regex module in python. I tried to get my program to match the following pattern from a line of text that the user inputs:
a number between 8-13 "/" a number between 0-15
For example: 8/2, 11/13, 10/9, etc.
The pattern that I came up with was:
upstream = re.compile(r'[8-9|1[0-3][/][0-9|1[0-5]')
However, this regex works with mixed results:
Enter a slot/port : 8/2
['8/2'] # This is correct
Enter a slot/port : 1/0
['1/0'] # This should print the "else" statement
Enter a slot/port : 8/15
['8/1'] # The output is incomplete
The problem seems to stem from the forward slash, but I am not sure. I do know that I need some assistance in solving this issue. If anyone can help me solve this, I would greatly appreciate it.
The complete script is below.
import re
pattern = re.compile(r'[8-9|1[0-3][/][0-9|1[0-5]')
upstream = input("Enter a slot/port : ")
if re.search((pattern), upstream):
print(re.findall(pattern, upstream))
else:
print("We have a problem")
Thanks in advance :)