Reputation: 808
I am trying to find the currency in strings like "NTE $22,539,420.00"
I tried to use several regular expressions including ^\s*[\+-]?\s?\$?\s?(\d*\.?\d{2}?){1}$
but none of them seem to work. Does anyone have any suggestions or reasons why the above would not work.
Thanks, Jim
Upvotes: 2
Views: 6073
Reputation: 652
Just stumbled upon this old post...
Use \p{Sc}
as suggested in this post.
Upvotes: 0
Reputation: 19214
You are matching the beginning of the string by using ^, and the beginning of your string does not start with any whitespace. You are also not taking into consideration the ',' characters.
Try this \s*[+-]?\s?\$?\s?((\d{1,3},?)+.?\d{2}?)
Upvotes: 3
Reputation: 15270
Could you just dump all the formatting (except for the decimal) like so?
string money = "NTE $22,539,420.00";
string scrubbed = Regex.Replace(money, @"[^0-9\.]", string.Empty);
At this point, scrubbed contains 22539420.00
.
Upvotes: 6
Reputation: 7946
The first part of the regex matches the start of the string, followed by zero or more spaces, followed possibly by a sign, followed possibly by a '$'... and then some digits. Nowhere is there any accommodation for the "NTE" part of your string. Also, I don't see any accommodation for the commas in your currency format.
Matching floating point can be a bit tricky. Don't forget about possibilities such as $.02.
Upvotes: 0