简体   繁体   English

Python正则表达式不匹配

[英]Python regular expression not matching

This is one of those things where I'm sure I'm missing something simple, but... In the sample program below, I'm trying to use Python's RE library to parse the string "line" to get the floating-point number just before the percent sign, ie "90.31". 这是我确定缺少简单的东西之一,但是...在下面的示例程序中,我试图使用Python的RE库解析字符串“ line”以获取浮点数百分号前的数字,即“ 90.31”。 But the code always prints "no match". 但是代码始终显示“不匹配”。

I've tried a couple other regular expressions as well, all with the same result. 我也尝试了其他两个正则表达式,所有结果都相同。 What am I missing? 我想念什么?

#!/usr/bin/python
import re
line = '    0 repaired, 90.31% done'
pct_re = re.compile(' (\d+\.\d+)% done$')
#pct_re = re.compile(', (.+)% done$')
#pct_re = re.compile(' (\d+.*)% done$')
match = pct_re.match(line)
if match: print 'got match, pct=' + match.group(1)
else: print 'no match'

match only matches from the beginning of the string. match仅从字符串开头开始匹配。 Your code works fine if you do pct_re.search(line) instead. 如果您pct_re.search(line)则您的代码可以正常工作。

You should use re.findall instead: 您应该改用re.findall

>>> line = '    0 repaired, 90.31% done'
>>> 
>>> pattern = re.compile("\d+[.]\d+(?=%)")
>>> re.findall(pattern, line)
['90.31']

re.match will match at the start of the string. re.match将在字符串的开头匹配。 So you would need to build the regex for complete string. 因此,您需要为完整的字符串构建正则表达式。

try this if you really want to use match: 如果您确实想使用match,请尝试以下方法:

re.match(r'.*(\d+\.\d+)% done$', line)

r'...' is a "raw" string ignoring some escape sequences, which is a good practice to use with regexp in python. r'...'是一个“原始”字符串,忽略了一些转义序列,这是在python中与regexp一起使用的好习惯。 – kratenko (see comment below) –克拉滕科(见下面的评论)

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

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