【发布时间】:2018-04-26 06:57:18
【问题描述】:
我正在使用正则表达式从 interest at the rate of ten percent (10%) 中使用关键字“interest at the rate”查找值
我试过了
re.compile(r'interest at the rate\s+((?:\w+(?:\s+|$)){3})').findall(r.decode('utf-8'))
并获得['of ten percent ']。
现在,我试过了
re.compile(r'interest at the rate of\s+((?:\w+(?:\s+|$)){3})').findall(r.decode('utf-8'))
但是,我得到的只是一个空值,[]。
如何从上面的行中得到数字 10?我想在关键字后面捕获三到四个单词并获取整数值。
【问题讨论】:
-
您的意思是您需要在搜索字符串后的括号内获取数值吗?试试
r'interest at the rate [^(]*\((\d[\d.]*)' -
或者,如果你需要这样的整个字符串,
r'interest at the rate\s+[^(]*\(\d[\d.]*%\)' -
@WiktorStribiżew 为什么不添加作为答案而不是在其工作时发表评论?
-
@VikasDamodar 在这种情况下,我不确定 OP 想要什么。
.+也可以,但可能不是 OP 需要的。 -
@dhinar,见
r'interest at the rate (\w+(?:\s+\w+){2,3})\s*\((\d[\d.]*)[^)]*\)',是你想要的吗?
标签: python regex keyword matching