Jim
Jim

Reputation: 808

.Net Regular Expression Currency

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

Answers (5)

Markus
Markus

Reputation: 652

Just stumbled upon this old post...

Use \p{Sc} as suggested in this post.

Upvotes: 0

NerdFury
NerdFury

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

arcain
arcain

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

Tuco
Tuco

Reputation: 1630

Try this one:

^[^\d]+(?:\d{1,3}[.,])+\d{2}$

Upvotes: 1

Codie CodeMonkey
Codie CodeMonkey

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

Related Questions