python - Using the Regex OR operator to accommodate user input of "A" or "An" -


i attempting validate user input of either 'a' | 'an' satisfy if statement. if not satisfied, elif block check see if second word "about", if not "about", check "anyone". unfortunately 'about' & "anyone" both start letters 'a' or 'an' needed add 'space' after end of 'a' , 'an' allow regex detect difference.

# receive user input. secrets = {} secrets['text'] = request.get.get('text')  regex_a = re.compile("(a|an)") regex_about = re.compile('about') regex_anyone = re.compile('anyone')  # second word secrets[text] secondword = secrets['text'].split()[1] # if 2nd word == 'a/an' if regex_a.match(secondword):     return httpresponse("text (a) or (an)")  # else if 2nd word == elif regex_about.match(secondword):     return httpresponse("second word (about)")  elif regex_anyone.match(secondword):     return httpresponse("second word (anyone)")  else:     return httpresponse("failed interpret user input") 

the current regex ("(a|an)") returns text (a) or (an) when user inputs "about" or "anyone" second word, expected.

so tried ("(a\s|an\s)") returns failed interpret user input when input second word 'a' or 'an'. returns correct response 'about' & 'anyone'. confusing...

i tried ("(a_|an_)") returns same results previous test.

apart these 3 tests have attempted many others, not list them here there far many.

you can use:

regex_a = re.compile("(a|an)$") 

that way telling regex string needs end right there match.

the regex ("(a\s|an\s)") not work never because expects substrings 'a ' , 'an ' match, , problem split() in secondword = secrets['text'].split()[1] returns whitespace-trimmed strings.