Home > database >  Regex find all money related value in a long string
Regex find all money related value in a long string

Time:04-02

I am trying to extract money-related value from a long string. I can match the following format using (?:cad|[$]|usd)\s*\d ?

$1000
cad 1000 
usd 1000
  1. I have trouble with the one with 1000 separators, like $1,000.
  2. I tried to add (\.\d ) to account for the optional digit and it is not working.
  3. If I try to account for another convention like 1000$ and 1000cad, is it better to do a separate regex for the reversed symbols and amount?

test1 = 'Today is 23, I will pay you $10,000.78 today by 7'

expect = $10,000.78

Thanks in advance.

CodePudding user response:

Use a regex tester to try stuff out. One good one is https://regex101.com/.

You need to use a character class [] like so:

(?:cad|[$]|usd)\s*[\d,.]

... and get rid of the ? which tells the regex to stop after the first digit.

If you want to check for optional stuff afterward, add an optional grouping:

(?:cad|[$]|usd)\s*[\d,.] (?:cad|usd|\$)?

... in this case, the trailing ? means "optional". Additionally, I recommend using the "i" (case insensitive) regex switch.

Foul

CodePudding user response:

You could write an alternation | to match both formats and use whitespace boundaries to prevent partial matches.

For matching the money you can use a character class [\d,.] to repeat matching a digit, comma or dot if you know that the money is always in the expected format and will never be for example $,,

(?<!\S)(?:(?:cad|[$]|usd) ?[\d,.] |[\d.,] (?:cad|[$]|usd))(?!\S)

See a regex 101 demo.

  • Related