mike
mike

Reputation: 3519

Python logical NOT operator for regular expressions

I tried searching for this answer online but I haven't found any luck. I am wondering if python supports a logical not operator (usually '!') in other languages for an if statement, or any control statement really. For example, I am looking to achieve this functionality.

if !(re.search('[0-9]', userInputVariable):
    fix error and report to user

...Proceed with rest of code...

Where basically if the user does not input a number I correct it to a default value and proceed with script

Thanks!

Upvotes: 1

Views: 3181

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

You're looking for the not operator.

But that's not how you check for a number.

try:
  int(userInputVariable)
except ValueError:
  print "Not a number"

...

if not userInputVariable.isdigit():

Upvotes: 6

Related Questions