开发者

.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 inc开发者_如何学Pythonluding ^\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


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.


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}?)


Try this one:

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


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.


Just stumbled upon this old post...

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜