Regex to extract all currency values

Hi All, I would like to seek some help in extracting all the currency values from the payment advice below:

20230511 1234567890 9,199.87 PHXXXX/PUXXXXX
20230511 9876543210 14,228.20 PHXXXX/PUXXXXX
20230511 9876543210 142,228.20 PHXXXX/PUXXXXX
20230511 9876543210 1,424,228.20 PHXXXX/PUXXXXX
20230511 9876543210 500.40 PHXXXX/PUXXXXX
20230511 9876543210 50.40 PHXXXX/PUXXXXX
20230511 9876543210 0.40 PHXXXX/PUXXXXX
20230511 9876543210 2 PHXXXX/PUXXXXX

tried the regex formula from this link but somehow it did not work…

import re

regex_payment_amount = r'\$(?:(?:[1-9][0-9]{0,2})(?:,[0-9]{3})+|[1-9][0-9]*|0)(?:[.,][0-9][0-9]?)?(?![0-9]+)'
  
payment_amounts = re.findall(regex_payment_amount, text)

print(payment_amounts)

\$ - Looking to match $ but the text has no $

Hi, @pkdvalis , I tried removing ‘$(?: , was not able to extract the payment amount as well

regex is hard. You can use this site to test and build: https://regex101.com/

chatGPT wrote this for me: r'\b([\d,]+\.\d+)\b'

But I would not use regex in this case. I would just use split() and you know the 3rd chunk is the value.

@pkdvalis , thanks for the help! appreciate it!

1 Like