import re
regex = r"\bTotal :.*[\r\n]+(\d+(?:\.\d+))\b"
test_str = ("Total :(Dirham Four Hundred Ninety Six and Sixty Five fils Only)\n\n"
"496.65\n\n"
"Best Regards,\n"
"y approve the proposal and arrange the payment, accordingly we will provide you the tax\n"
"invoice .")
print(re.findall(regex, test_str, re.IGNORECASE))
您可以匹配
total :
并通过在匹配行的其余部分后匹配1个或多个换行来捕获组中的值解释
\bTotal :.*
匹配total :
和该行的其余部分[\r\n]+
匹配1+换行符(\d+(?:\.\d+))
捕获组1,将一个数字与可选的小数部分匹配\b
单词边界Regex demoPython demo
示例代码
输出
相关问题 更多 >
编程相关推荐