Reputation: 31
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
(\.\d+)
to account for the optional digit and it is not working.test1 = 'Today is 23, I will pay you $10,000.78 today by 7'
expect = $10,000.78
Thanks in advance.
Upvotes: 0
Views: 785
Reputation: 163362
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.
Upvotes: 1
Reputation: 654
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
Upvotes: 1