简体   繁体   中英

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. The amount is in next line. Thanks in advance!

Total:(Dirham Four Hundred Ninety Six and Sixty Five fils Only)

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)

You can match total: and capture the value in a group by matching 1 or more newlines after matching the rest of the line.

\bTotal :.*[\r\n]+(\d+(?:\.\d+))\b

Explanation

  • \bTotal:.* Match total: and the rest of the line
  • [\r\n]+ Match 1+ newlines
  • (\d+(?:\.\d+)) Capture group 1, match a digit with an optional decimal part
  • \b A word boundary

Regex demo | Python demo

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

['496.65']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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