How to remove dollar format with regex - regex

I am trying to remove the dollar format from the string '$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?
^\$

Related

REGEXP to grab all text before second underscore, including second underscore

So I have strings that come across like this:
GRF_STHB_010_00
ABC_AB9_004_01
BGH_NP2_002_03
AG2_BVT_007_010
The text before the first underscore can be any combo of Letters or Numbers.
The text before the second underscore can also be any combo of letters or numbers.
I want to be able to grab the whole string before the 2nd underscore, including the second underscore.
I have come up with this for now:
^([^\d]*)
It works for the first one, and finds:
GRF_STHB_
But for the other two it stops at a number that it finds:
ABC_AB
BGH_NP
AG
I need this to work in REGEXP because this is being included in a spreadsheet for grabbing data.
How can I adjust it so that it works with numbers and would have a result of:
GRF_STHB_
ABC_AB9_
BGH_NP2_
AG2_BVT_
Here is a quick tester for anyone that can help:
regexpal.com
Thanks!
You can use this regex for this:
^([^_]*_){2}
Online Demo: http://regex101.com/r/cX7hL7
You can use this :
^[^_]*_[^_]*_
You can use this regex:
^([^_]*_[^_]*)_.*$
Demo

Regular expression for a list of items separated by comma or by comma and a space

Hey,
I can't figure out how to write a regular expression for my website, I would like to let the user input a list of items (tags) separated by comma or by comma and a space, for example "apple, pie,applepie". Would it be possible to have such regexp?
Thanks!
EDIT:
I would like a regexp for javascript in order to check the input before the user submits a form.
What you're looking for is deceptively easy:
[^,]+
This will give you every comma-separated token, and will exclude empty tokens (if the user enters "a,,b" you will only get 'a' and 'b'), BUT it will break if they enter "a, ,b".
If you want to strip the spaces from either side properly (and exclude whitespace only elements), then it gets a tiny bit more complicated:
[^,\s][^\,]*[^,\s]*
However, as has been mentioned in some of the comments, why do you need a regex where a simple split and trim will do the trick?
Assuming the words in your list may be letters from a to z and you allow, but do not require, a space after the comma separators, your reg exp would be
[a-z]+(,\s*[a-z]+)*
This is match "ab" or "ab, de", but not "ab ,dc"
Here's a simpler solution:
console.log("test, , test".match(/[^,(?! )]+/g));
It doesn't break on empty properties and strips spaces before and after properties.
This thread is almost 7 years old and was last active 5 months ago, but I wanted to achieve the same results as OP and after reading this thread, came across a nifty solution that seems to work well
.match(/[^,\s?]+/g)
Here's an image with some example code of how I'm using it and how it's working
Regarding the regular expression... I suppose a more accurate statement would be to say "target anything that IS NOT a comma followed by any (optional) amount of white space" ?
I often work with coma separated pattern, and for me, this works :
((^|[,])pattern)+
where "pattern" is the single element regexp
This might work:
([^,]*)(, ?([^,]*))*
([^,]*)
Look For Commas within a given string, followed by separating these. in regards to the whitespace? cant you just use commas? remove whitespace?
I needed an strict validation for a comma separated input alphabetic characters, no spaces. I end up using this one is case anyone needed:
/^[a-z]+(,[a-z]+)*$/
Or, to support lower- and uppercase words:
/^[A-Za-z]+(?:,[A-Za-z]+)*$/
In case one need to allow whitespace between words:
/^[A-Za-z]+(?:\s*,\s*[A-Za-z]+)*$/
/^[A-Za-z]+(?:,\s*[A-Za-z]+)*$/
You can try this, it worked for me:
/.+?[\|$]/g
or
/[^\|?]+/g
but replace '|' for the one you need. Also, don't forget about shielding.
something like this should work: ((apple|pie|applepie),\s?)*

SSN Regex for 123-45-6789 OR XXX-XX-XXXX

Can someone provide me a regex for SSN that matches either
123-45-6789
OR
XXX-XX-XXXX
I currently have ^\d{3}-?\d{2}-?\d{4}$ which matches the first expression, but I need to add the second expression to it as an alternative.
Thanks!
To strictly answer you question:
^(123-45-6789|XXX-XX-XXXX)$
should work. ;-)
If you read the section "Valid SSNs" on Wikipedia`s SSN article then it becomes clear that a regex for SSN validation is a bit more complicated.
Accordingly a little bit more accurate pure SSN regex would look like this:
^(?!(000|666|9))\d{3}-(?!00)\d{2}-(?!0000)\d{4}$
(^\d{3}-?\d{2}-?\d{4}$|^XXX-XX-XXXX$) should do it.
---- EDIT ----
As Joel points out you could also do ^(\d{3}-?\d{2}-?\d{4}|XXX-XX-XXXX)$ which is a little neater.
So you currently have: ^\d{3}-?\d{2}-?\d{4}$
What you need is to allow any of those numeric blocks to be "X"s instead. This is also fairly simple as a regex - just adapt your existing one to have X instead of \d in each of the three places it occurs: X{3}-?X{2}-?X{4}
You won't want to be combining a numeric code with and X code, so you just need to allow either one case or the other, so wrap them up in brackets and us a pipe character to specify one or the other, like so:
^((\d{3}-?\d{2}-?\d{4})|(X{3}-?X{2}-?X{4}))$
You'll probably also want to allow upper- or lower-case X. This can be specified using [Xx] or by making the whole thing case insensitive, using the i modifier outside the regex.
Then it can be
/^[\dX]{3}-?[\dX]{2}-?[\dX]{4}$/
if you want x to be valid too, you can add the i modifier to the end:
/^[\dX]{3}-?[\dX]{2}-?[\dX]{4}$/i
On second thought, the regex above will accept
123-xx-xxxx
as well, so depending on whether you want this form to be accepted or not, you can
use your original form "or" the other form:
/^(\d{3}-?\d{2}-?\d{4})|(xxx-xx-xxxx)$/i
A more generic match would be:
(^[^-]{3}-?[^-]{3}-?[^-]{4}$)
This would match any sequence of characters other than "-" in 3-3-4 char configuration. For example:
my #str = qw/
1adfasdfa
adsfaouaosd90890
111-232-adafd
xXX-232-1234
111-222-4444
$$%-AF#-131#
/;
foreach(#str)
{
print "$_\n" if /^[^-]{3}-?[^-]{3}-?[^-]{4}$/;
}
^\d{3}-?\d{2}-?\d{4}$|^XXX-XX-XXXX$

Regular Expression to find sequences of lowercase letters joined with underscore

I can't seem to make my regular expression work.
I'd like to have some alpha text, no numbers, an underscore and then some more aplha text.
for example: blah_blah
I have an non-working example here
^[a-z][_][a-z]$
Thanks in advance people.
EDIT: I apologize, I'd like to enforce the use of all lower case.
^[a-z]+_[a-z]+$
Try this:
[A-Za-z]+_[A-Za-z]+
Lowercase :
[a-z]+_[a-z]+
You just need:
[a-z]+_[a-z]+
or if it needs to be an entire line:
^[a-z]+_[a-z]+$
Try:
^[a-z]+_[a-z]+$
Depending on which flavor of regex you're using there are a different possibilities:
^[A-Za-z]+_[A-Za-z]+$
^\a+_\a+$
^[[:alpha:]]+_[[:alpha:]]+$
The first form being the most widely accepted.
Your example suggests you're looking for things exactly like "blah_foo" and don't want to extract it from strings like "Hey blah_foo you". If this is not the case, you should drop the "^" (match the beginning of the string) and "$" (match the end of the string)

Regular expression - what is my mistake?

I would like to match either any sequence or digits, or the literal: na .
I am using:
"^\d*|na$"
Numbers are being matched, but not na.
Whats my mistake?
More info: im using this in a regular expression validator for a textbox in aspnet c#.
A blank entry is ok.
It's because the expression is being read (assuming PCRE):
"^\d*" OR "na$"
Some parentheses would take care of that in a jiff. Choose from (depending on your needs):
"^(\d+|na)$" // this will capture the number or na
"^(?:\d+|na)$" // this one won't capture
Cheers!
The | operator have a higher precedence than the anchors ^ and $. So the expression ^\d*|na$ means match ^\d* or na$. So try this:
^(\d*|na)$
Or:
^\d*$|^na$
Perhaps ^(?:\d*|na)$ would be better. What language/engine? Also, please show the input and, if possible, the snippet of the code.
Also, it is possible that you aren't matching "na" because there is a new line after it. The digits wouldn't be affected because you did not specify a $ anchor for them.
So, depending on the language and how the input is acquired, there might be new-line between "na" and the end of the string, and $ won't match it unless you turn on multi-line match (or strip the string of the new line).
This may not be the best or most elegant way to fix it, but try this:
"^\d*|[n][a]$"