I have written a small regex for javascript. It should only accept numbers separated by commas.
Valid examples are:
1 single value allowed
1,278,3780,50
1,56,90, (trailing comma allowed)
Invalid examples are:
1,45 67
1, gj, + (any special character and characters)
The regex is: /^[\d|\,]+/g
However, it also accepts | (pipe character).
Like: 1|46|6778|567
What am I doing wrong? What did I miss?
Please follow this link to my regex
You don't need pipe (|) and escaping characters within character class. Also as a proper way you can use following regex:
/^(?:\d+\,)+\d+$/g
Debuggex Demo
As i missed your edit if the trailing comma is a valid case you can simply use following regex :
^(\d+,?)+$
Try this -
^\d+\,(?:\d+\,?)+$
Demo
EDIT:
With changed requirements -
^\d+(?:,\d+)*,?$
Demo here
To match a number separated by comma:
(\d+,?)+
The correct regex is as follows:
^\d+(?:,\d+)*,?$
This will match the cases specified:
A single number (1)
A series of numbers delimited by commas (1,2,3)
An optional trailing comma (1,, 1,2,3,4,)
Related
I tried with this regex to match floating values:
(^\d{0,11}$)|^\d{0,11}([.]\d{0,6})?$
However, I don't want to allow strings like 12., i.e. a number with a dot at the end.
Please me give me a suggestion.
You may use
^\d{0,11}(?:\.\d{1,6})?$
If you use \d{0,6}, the pattern may match an empty string. Note that it is not recommended to test JS regex with RegexStorm.net as it only supports .NET regex and uses CRLF line endings.
Details
^ - start of string
\d{0,11} - zero to eleven digits
(?:\.\d{1,6})? - an optional sequence of
\. - a dot
\d{1,6} - 1 to 6 digits
$ - end of string.
See the regex demo.
How about you restrict the count of the decimal part.. like this
^\d{0,11}(?:\.\d{1,6})?$
I'm trying to create an expression to collect anything but digits and the character *. I tried to use the expression \D[^*] but somehow it's retrieving the first digit after blank espaces. I tried this expression with the string 1234 1234 1234 **** and the matches were: ' 1', ' 1'. Can anyone tell me why would the expression collect the digits with the blank spaces?
Thank you.
Your regex ('\D[^*]') will match a Space (thats' not a digit) followed by a digit (that's not a star '*').
You can do several Things, the easiest is to include '\d' in the character Group, then it will Work, because both '\d' and '*' are excluded:
/[^\d*]/g
Now it will only match Spaces in your example.
A square brackets in regex are a set, and a square bracket with ^ mean not in set.
The \d should also be inside the brackets:
[^\d*]
https://regex101.com/r/WizvVh/1
I want to validate some strings.
Rules:
String can be contain A-Z, 0-9, "_", "!", ":".
If string contains 2x special characters, eg, "__" or "!!" or "K:KD:E" must return false.
Examples
Legitimate matches
FX:EURUSD
FX_IDC:XAGUSD
NYMEX_EOD:NG1!
Invalid matches:
0-BITSTAMP:BTCUSD - contains a minus sign)
2.5*AMEX:VXX+AMEX:SVXY - contains a *, a + and 2x ":"
AMEX:SPY/INDEX:VIX - contains a /
You can use this negative lookahead based regex:
/^(?:[A-Z0-9]|([_!:])(?!.*\1))+$/gm
RegEx Demo
([_!:])(?!.*\1) will ensure there is no repetition of special characters.
I would first start out with regex to remove all strings containing invalid characters:
/[^A-Z0-9_!:]/
I would then use this to check for duplicates:
/(_.*_)|(!.*!)|(:.*:)/
These can be combined to give:
/([^A-Z0-9_!:])|(_.*_)|(!.*!)|(:.*:)/
This can be seen in action here.
And here is a JSFiddle showing it working.
I eventrually used pattern
var pattern = /^[a-zA-Z_!0-9]+:?[a-zA-Z_!0-9]+$/;
I want to get the last string between special characters. I've done for square bracket as \[(.*)\]$
But, when I use it on something like Blah [Hi]How is this[KoTuWa]. I get the result as [Hi]How is this[KoTuWa].
How do i modify it to get the last stringthat is KotuWa.
Also, I would like to generalise to general special characters, instead of just matching the string between square brackets as above.
Thanks,
Sai
I would do this:
[^[\]]+(?=][^[\]]*$)
Debuggex Demo
To extend this to other types of brackets/special chars, say I also wanna match curly braces { and double quotes ":
[^{}"[\]]+(?=["\]}][^{}"[\]]*$)
Debuggex Demo (I added the multi-line /m only to show multiple examples)
Here is one way to do it:
\[([^\[]*)\]$
You can require that the string between brackets does not contain brackets:
Edit: thanks to funkwurm and jcubic for pointing out an error. Here's the fixed expression:
\[([^[]+)\][^\[]*$
If you need to use other separators than brackets, you should:
replace the \[ and \] with your new separators
replace the negative character classes with your beginning separator.
For example, assuming you need to use the separators <> instead of [], you'd do this:
<([^<]+)>[^\>]*$
I'm trying to write a regular that will check for numbers, spaces, parentheses, + and -
this is what I have so far:
/\d|\s|\-|\)|\(|\+/g
but im getting this error: unmatched ) in regular expression
any suggestions will help.
Thanks
Use a character class:
/[\d\s()+-]/g
This matches a single character if it's a digit \d, whitespace \s, literal (, literal ), literal + or literal -. Putting - last in a character class is an easy way to make it a literal -; otherwise it may become a range definition metacharacter (e.g. [A-Z]).
Generally speaking, instead of matching one character at a time as alternates (e.g. a|e|i|o|u), it's much more readable to use a character class instead (e.g. [aeiou]). It's more concise, more readable, and it naturally groups the characters together, so you can do e.g. [aeiou]+ to match a sequence of vowels.
References
regular-expressions.info/Character Class
Caveat
Beginners sometimes mistake character class to match [a|e|i|o|u], or worse, [this|that]. This is wrong. A character class by itself matches one and exactly one character from the input.
Related questions
Regex: why doesn’t [01-12] range work as expected?
Here is an awesome Online Regular Expression Editor / Tester! Here is your [\d\s()+-] there.
/^[\d\s\(\)\-]+$/
This expression matches only digits, parentheses, white spaces, and minus signs.
example:
888-111-2222
888 111 2222
8881112222
(888)111-2222
...
You need to escape your parenthesis, because parenthesis are used as special syntax in regular expressions:
instead of '(':
\(
instead of ')':
\)
Also, this won't work with '+' for the same reason:
\+
Edit: you may want to use a character class instead of the 'or' notation with '|' because it is more readable:
[\s\d()+-]
Try this:
[\d\s-+()]