final_str = 'hello your total sale Fare Rs 741 your bill pls'
r = re.compile(r'\bSale\b|\bFare\b|\fare\b|\bRs\b', flags=re.I | re.X)
p=r.findall(final_str)
lis_con = p
matched = ''.join(lis_con)
res = final_str
list_of_words = res.split()
gotdata = list_of_words[list_of_words.index(lis_con[0]) + 1]
print(gotdata)
到目前为止,我已经尝试过但未达到结果,输出期望是它应该检查字典中存在的所有单词列表及其返回数字,例如,它匹配Sale,Fare,Rs,因此它返回下一个单词,但是我想要返回我的情况下唯一的数字,它应该返回45。我想我需要检查isDigit()任何高度赞赏的帮助。
您可以使用
\b(?:Sale|Fare|Rs)\s+(\d+(?:,\d+)*(?:\.\d+)?)
请参阅regex演示。细节:
\b
-单词边界(?:Sale|Fare|Rs)
- Sale
,Fare
或Rs
串\s+
-一个或多个空格(\d+(?:,\d+)*(?:\.\d+)?)
-第1组:一个或多个数字,然后是零个或多个重复的逗号和一个或多个数字,然后是一个点和一个或多个数字的可选出现。参见Python演示:
import re
final_str = 'hello your total sale Fare Rs 741 your bill pls and Sale 2,804.00'
r = re.compile(r'\b(?:Sale|Fare|Rs)\s+(\d+(?:,\d+)*(?:\.\d+)?)', flags=re.I)
gotdata = r.findall(final_str)
print(gotdata)
# => ['741', '2,804.00']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句