You almost have it, you just left out 0 and forgot the quantifier.
word.matches("^[0-9,;]+$")
Answer from Anomie on Stack OverflowYou almost have it, you just left out 0 and forgot the quantifier.
word.matches("^[0-9,;]+$")
You are 90% of the way there.
^[0-9,;]+$
Starting with the carat ^ indicates a beginning of line.
The [ indicates a character set
The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ;.
The closing ] indicates the end of the character set.
The plus + indicates that one or more of the "previous item" must be present. In this case it means that you must have one or more of the characters in the previously declared character set.
The dollar $ indicates the end of the line.
[Regex] Help with a regex for single letters separated by comma/whitespace?
Regex to check if string has special characters except dash and comma and split
RegexMatcher for Special Characters
how to match all special characters except a comma
My regex: ([a-zA-Z],? *)+
The desired input is something like "A, B, C, D". But of course I want to allow input where the letters aren't capitalized, or there's more than one space between a letter+comma, or no space, etc.
Here's the regex I have, but it's not quite right because input such as "A B C D" is still considered valid.
([a-zA-Z],? *)+ which as I understand it (I'm new to regex) means: contains any letter from a to z, case insensitive, followed by zero or one comma, followed by 0 or more space(s), and then 1 or more of this entire pattern.
Now the issue I think has to do with the "zero or one comma" because I want to allow the input to have multiple letters eg. "A, B, C" but the last character shouldn't be followed by a comma, because that's weird. It also needs to match with single letter input which doesn't have a comma either. So what I want is it to be like a letter followed by a comma only if this isn't the last letter or the only letter... but I don't know how to do that with regex.
EDIT: Here is a new regex I made. I think this works: ([a-zA-Z] *, *)*([a-zA-Z] *)
This will match everything that is not numeric or not a comma or period (decimal point)
var result = str.replace(/[^0-9\.,]/g, "");
var check = yourString.match(/[^0-9,\.]/);
Here check will be 'null' if the string does not contain a character different to a number, a comma or a point. If the string has any of these characters, check will be an Array. You could test this in this way
if (check === null ) { console.log('No special characters present') };
if (typeof check === 'Array' ) { console.log('Special characters present') };
Try if something like this would match your needs:
^(?:([A-D])(?!.*?\1),)*[A-D]$
If there's more than just one [A-D], preceding ones must be followed by a comma, captures prior [A-D] to \1 and checks if not followed by itself using a negative lookahead.
See test at regex101.com; Regex FAQ
^(?!.*?([A-D]).*?\1)A-D*$
Try this.See demo.
https://regex101.com/r/uC8uO6/8
var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g
Should work
Also may want to have a minimum length i.e. 6 characters
var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]{6,}$/g
a sleaker way to match special chars:
/\W|_/g
\W Matches any character that is not a word character (alphanumeric & underscore).
Underscore is considered a special character so add boolean to either match a special character or _
Many thanks in advance if you can spare a quick second with this seemingly simple puzzle.
I have the following strings and need to extract 2nd to last digit/digit before the last comma. I've tried dozens of different attempts but it appears this one is beyond me so happy to learn from the correct regex with the aim of being able to pass the knowledge on. As this is within a webapp, I suspect the engine being used will very likely be Javascript.
INPUTS:
0, 2, 31, 3, 6, 88, 49, 3, 5, 3, 7
DESIRED OUTPUTS:
2
6
8
3
SOME NAIVE ATTEMPTS:
(?<=\d+)([A-Z]{1})$
([0-9]*)+([A-Z]*)
([0-9]+)([A-Z])
Huge thanks once again if you can help as I've been pulling my hair out for ages!
Warmest regards.