[英]How to extract the next line after a specific keyword when there are words in between using regex in Python?
I want to extract the amount 495.65 after matching the keyword total.我想在匹配关键字总数后提取金额 495.65。 The amount is in next line.
金额在下一行。 Thanks in advance!
提前致谢!
Total:(Dirham Four Hundred Ninety Six and Sixty Five fils Only)总计:(仅迪拉姆四百九十六和六十五菲尔)
496.65 496.65
Best Regards, y approve the proposal and arrange the payment, accordingly we will provide you the tax invoice.最好的问候,您批准建议并安排付款,因此我们将为您提供税务发票。
re.findall('(?<=total :)((.*){2})', string, re.IGNORECASE)
The output is: (Dirham Four Hundred Ninety Six and Sixty Five fils Only) output 是:(仅迪拉姆四百九十六和六十五 fils)
You can match total:
and capture the value in a group by matching 1 or more newlines after matching the rest of the line.您可以匹配
total:
并在匹配该行的 rest 后匹配 1 个或多个换行符来捕获组中的值。
\bTotal :.*[\r\n]+(\d+(?:\.\d+))\b
Explanation解释
\bTotal:.*
Match total:
and the rest of the line \bTotal:.*
匹配total:
和该行的 rest[\r\n]+
Match 1+ newlines [\r\n]+
匹配 1+ 个换行符(\d+(?:\.\d+))
Capture group 1, match a digit with an optional decimal part (\d+(?:\.\d+))
捕获组 1,匹配带有可选小数部分的数字\b
A word boundary \b
一个词的边界Regex demo |正则表达式演示| Python demo
Python 演示
Example code示例代码
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))
Output Output
['496.65']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.