Regex Pattern for Currency within String?

Hi all…:slight_smile:

I’m looking for a regex pattern that would return true for a string like this:
$argument = "FNB :-) R350.00 paid to cheq a/c..887782 @ Metlife ATM.Ref.1198garfield. 29Dec 11:06";

I found a pattern to test against the actual currency, but not within a string:

$regex = /^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$/;

Is there any 100% working and tested regex pattern that can be used for the above string?

I’m currently using a workaround, like so:

$amount = $argument.split(' ');
console.log($regex.test($amount[2]));

It works, but all hell breaks loose if that text formatting (the payment notification) were to change (my system is relying, depending, on that format! Really unstable I know…that’s why I was thinking Regex)

What is not clear from your question is what is or is not working for you?

My answer to “Is there any 100% working and tested regex pattern that can be used for the above syntaxes?” would be, probably, yes
But don’t ask me to build such a complex Regex please!

In the past I have used http://regexlib.com/ to look for a pattern and worked from there.

Of course not, I wouldn’t even spend my own time reinventing the wheel. Sorry for the unclear question - I’m simply asking if anyone came across a working, tested regex to match a string if it has currency in it…

Have a look at the currency patterns on regexlib. (see link above)

1 Like