Skip to main content
 首页 » 编程设计

用于正则表达式的 Python 逻辑 NOT 运算符

2025年05月04日26lori

我试着在网上搜索这个答案,但我没有找到任何运气。我想知道 python 是否支持在其他语言中用于 if 语句或任何控制语句的逻辑非运算符(通常是“!”)。例如,我希望实现此功能。

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

基本上,如果用户没有输入数字,我会将其更正为默认值并继续执行脚本

谢谢!

请您参考如下方法:

您正在寻找 not 运算符。

但这不是检查数字的方式。

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

...

if not userInputVariable.isdigit():