简体   繁体   English

为什么提供的正则表达式返回true?

[英]Why does this provided regular expression return true?

I would like to know why following regular expression returns true: 我想知道为什么以下正则表达式返回true:

reg = re.compile (r'[0-9]%')
reg.search ("50%")

[0-9] would match any single digit, in this case 5. But then 0 doesn't match %, so it should return false, but it returns true. [0-9]将匹配任何单个数字,在这种情况下为5。但是0不匹配%,因此它应返回false,但返回true。

My code might have syntax errors, but you get the gist of it. 我的代码可能有语法错误,但是您可以理解。

reg.search() matches the pattern anywhere in the string (so it matches the 0%). reg.search()会在字符串中的任何位置匹配模式(因此它匹配0%)。 If you want the entire string to match, try this: 如果您希望整个字符串都匹配,请尝试以下操作:

re.compile(r'^[0-9]%$') re.compile(r'^ [0-9]%$')

^ - matches the start of the string ^-匹配字符串的开头

$ - matches the end of the string $-匹配字符串的结尾

此正则表达式将匹配50%0%部分。

If you are searching for single-digit percentages inside a longer string, you could use a negative lookbehind : 如果要在较长的字符串中搜索个位数百分比,则可以在后面使用负数

In [171]: print(re.search('(?<!\d)\d%',"Foo is 5% complete"))
<_sre.SRE_Match object at 0xab302f8>

In [172]: print(re.search('(?<!\d)\d%',"Foo is 50% complete"))
None

In [173]: print(re.search('(?<!\d)\d%',"5% complete"))
<_sre.SRE_Match object at 0xab301a8>

In [174]: print(re.search('(?<!\d)\d%',"50% complete"))
None

As gfdunn2 mentioned, it does a 'rolling-match' of the entire string. 正如gfdunn2所提到的,它对整个字符串进行“滚动匹配”。 There are a couple things you can do to control it a bit better though. 您可以做一些事情来控制它更好一些。

The braces {} below can control how many characters you get, so it will give you much tighter matching. 下面的花括号{}可以控制您获得多少个字符,因此它将使您的匹配更加紧密。

>>> import re  

#exactly 1 digit and %
>>> test = re.compile(r'[0-9]{1}%')  
>>> print test.search("50%").group(0)  
0%  


#exactly 2 digits and %
>>> test = re.compile(r'[0-9]{2}%')  
>>> print test.search("50%").group(0)  
50%  


#one or more digits  
>>> test = re.compile(r'[0-9]+%')  
>>> print test.search("50%").group(0)  
50%  

#in the event you want to include floating point percentages  
>>> test = re.compile(r'[0-9.]+%')  
>>> print test.search("50.4%").group(0)  
50.4%

>>> print test.search("50.34%").group(0)
50.34%

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

相关问题 为什么此python正则表达式返回错误的字符串 - Why does this python regular expression return the wrong string 为什么我的正则表达式会为字符串中的每个字符返回元组? - Why does my regular expression return tuples for every character in a string? 为什么这个正则表达式不起作用? - Why does this regular expression not work? 为什么“not(True) in [False, True]”返回 False? - Why does "not(True) in [False, True]" return False? 为什么4 &lt;&#39;3&#39;在Python 2中返回True? - Why does 4 < '3' return True in Python 2? 为什么〜(真^假)返回-2 - why does ~(True ^ False) return -2 python 正则表达式未按预期返回值 - python regular expression does not return the value as expected 为什么同样的表达式在作为一行输入到 python 解释器时返回 True,而在脚本中运行时返回 False? - Why does this same expression return True in when entered as a line into the python interpreter and False when run in a script? 当 &#39;[] is []&#39; 和 &#39;{} is {}&#39; 返回 False 时,为什么 &#39;() is ()&#39; 返回 True? - Why does '() is ()' return True when '[] is []' and '{} is {}' return False? 为什么IN不评估我的正则表达式? - Why does IN not evaluate my regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM