0

I want to test for the entry into the terminal of a single word, i.e. 'Cat' (without anything at the end).

This allows "Cat     ":

re.match(r'[A-Z]{1}[a-z]{1,15}', x)

This doesn't allow even 'Cat' to be entered!:

re.match(r'[A-Z]{1}[a-z]{1,15}[^\s]', x)

What am I doing wrong? To clarify, it asks for an input and the person has to type Cat and hit enter, no spaces at the end. Thanks! :)

l'L'l
  • 44,951
  • 10
  • 95
  • 146
Her0Her0
  • 486
  • 1
  • 4
  • 12
  • 1
    You just need to add an `end-of-string` anchor i.e. `$` to your regex: `re.match(r'[A-Z]{1}[a-z]{1,15}$', x)` – Nick Feb 16 '20 at 04:24

1 Answers1

0

You can use x.replace(' ', ''). This allow even 'Cat ' and ' Cat' (or ' Cat ')

ChickenMinh
  • 39
  • 1
  • 2
  • 11