简体   繁体   English

Python中是否存在负正则表达式(否定)?

[英]Is there a negative regular expression in Python (negation)?

Let's say that I want to make a RE that describes all integers. 假设我想创建一个描述所有整数的RE。 By definition the expression is: 根据定义,表达式为:

(+|-)?[0-9]+

But the definition also matches these numbers : +0, -0, 0045 但该定义也匹配这些数字: +0, -0, 0045

The 0045 occasion can probably be solved using a lookback expression but how can I excusively exclude the +0 and -0 strings from the RE. 可以使用回顾表达式来解决0045场合,但是如何从RE中排除+0-0字符串。 I thought the syntax for this was ^(+0|-0 ) but that's probably a RE syntax for an other framework not Pythons'. 我认为这个语法是^(+0|-0 ),但这可能是其他框架的RE语法而不是Pythons'。

Then the case of zero can be handled separately. 那么零的情况可以单独处理。 Regular expression for zero or non-zero numbers: 零或非零数字的正则表达式:

0|[+-]?[1-9][0-9]*

The assumptions were: 假设是:

  • no superfluous heading zeroes. 没有多余的标题零。
  • no sign before zero 零之前没有迹象
  • superfluous plus sign allowed before non-zero numbers. 在非零数字之前允许多余的加号。

Outside a character group (with square brackets), the symbol ^ means the start of the string or line depending on the flag setting of re.MULTILINE . 在字符组外(带方括号),符号^表示字符串或行的开头,具体取决于re.MULTILINE的标志设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM