How to remove dollar format with regex
I am trying to remove the dollar format from the strin开发者_开发问答g '$1,109,889.23'. I tried using a regular expression with:
"[^\\d]"
but then I get the commas.
Any help? Thanks in advance.
You don't need a regex for this.
Just use lsParseCurrency
:
numericValue = lsParseCurrency('$1,109,889.23');
writeOutput(numericValue);
Example on trycf.com yields:
1109889.23
As per Leigh's commment below... if yer locale is something that doesn't use ,
and .
for the thousands and decimal separators (respectively)... make sure to specify the locale too, eg:
numericValue = lsParseCurrency('$1,109,889.23', 'en_us');
How about just doing a search and replace for ,
and $
?
but if you're going to do it.
[^\d.]+
I am using ColdFusion. The [^\d.] works great as eldarerathis mentioned above.
<cfset amt = '$1,109,889.23'>
<cfset newAmt = ReReplace(amt, "[^\d.]", "","ALL") >
<cfoutput>#newAmt#</cfoutput>
Since $
is a metacharacter in regex (it means "end of string" or "end of line", depending on the current settings), it needs to be escaped to \$
. But why use a regex at all if it's just one fixed character?
[\d,.]+
would give you the number part. Here is your example on Rubular.
I'm not sure I understand exactly what you want, but it seems that in your example you would want to end up with "1,109,889.23". If this is the case, why don't you simply select use [\d,.\x20]+
(note the period is not escaped, as it is in character set, and note the \x20 to select spaces if they use that instead of commas) to select just the number. If you want to select everything that is NOT part of the number, as your example indicates, then you would just search for [^\d,.\x20]
. This will work for any currency format, not just ones that use the dollar sign. It will also allow multiple types of punctuation, such as allowing spaces instead of commas to separate multiple numbers. However, I agree with Tim Pietzcker that a regular expression might not be the right tool for the job.
In Java:
String newString = oldString.replaceAll("$", "");
Would this RegEx work for you?
^\$
精彩评论