1

I'm trying to make my code loop back to the beginning until the user inputs something that matches the REGEX pattern.

For example:

userInput = raw_input('Enter text :')

if re.match("REGEX", userInput):
    #Do something

If the REGEX pattern does not match userInput, the code should ask them to enter userInput again until it matches the REGEX and there forth will do something. I am assuming a for loop is needed but I am not sure how to use it with REGEX.

<<< UPDATE >>>

Solution thanks to VKS:

while True:
    userInput = raw_input('Enter text :')
    if re.match("REGEX", userInput):
         break
Enigmatic
  • 3,902
  • 6
  • 26
  • 48

2 Answers2

1
while True:
    userInput = raw_input('Enter text :')
    if re.match("REGEX", userInput):
         break

You can do this simply

vks
  • 67,027
  • 10
  • 91
  • 124
0

Add a flag, set it to true, set it to false when a match occurs

do_loop = True

while do_loop:

    userInput = raw_input('Enter text :')

    if re.match("REGEX", userInput):
       #Do something
       do_loop = False
Radu Diță
  • 13,476
  • 2
  • 30
  • 34