Reputation: 1095
What is the regular expression to validate a comma delimited list like this one:
12365, 45236, 458, 1, 99996332, ......
Upvotes: 96
Views: 228967
Reputation: 3348
In real case scenarios you might need to match something more complex than digits. The best answer here suggests duplicating the expression in order to omit the final comma. This gets messy when you have a more complex list element.
The right way to go without duplicating is to use negative lookbehind like so:
^(\d+,?)+(?<!,)$
Keeping things simple - you can add \s
with the same way.
Upvotes: 0
Reputation: 11
If you had a more complicated regex, i.e: for valid urls rather than just numbers. You could do the following where you loop through each element and test each of them individually against your regex:
const validRelativeUrlRegex = /^(^$|(?!.*(\W\W))\/[a-zA-Z0-9\/-]+[^\W_]$)/;
const relativeUrls = "/url1,/url-2,url3";
const startsWithComma = relativeUrls.startsWith(",");
const endsWithComma = relativeUrls.endsWith(",");
const areAllURLsValid = relativeUrls
.split(",")
.every(url => validRelativeUrlRegex.test(url));
const isValid = areAllURLsValid && !endsWithComma && !startsWithComma
Upvotes: -1
Reputation: 57
The following will match any comma delimited word/digit/space combination
(((.)*,)*)(.)*
Upvotes: -1
Reputation: 162851
I suggest you to do in the following way:
(\d+)(,\s*\d+)*
which would work for a list containing 1 or more elements.
Upvotes: 133
Reputation: 125
In JavaScript, use split
to help out, and catch any negative digits as well:
'-1,2,-3'.match(/(-?\d+)(,\s*-?\d+)*/)[0].split(',');
// ["-1", "2", "-3"]
// may need trimming if digits are space-separated
Upvotes: 1
Reputation: 426
This regex extracts an element from a comma separated list, regardless of contents:
(.+?)(?:,|$)
If you just replace the comma with something else, it should work for any delimiter.
Upvotes: 31
Reputation: 637
I had a slightly different requirement, to parse an encoded dictionary/hashtable with escaped commas, like this:
"1=This is something, 2=This is something,,with an escaped comma, 3=This is something else"
I think this is an elegant solution, with a trick that avoids a lot of regex complexity:
if (string.IsNullOrEmpty(encodedValues))
{
return null;
}
else
{
var retVal = new Dictionary<int, string>();
var reFields = new Regex(@"([0-9]+)\=(([A-Za-z0-9\s]|(,,))+),");
foreach (Match match in reFields.Matches(encodedValues + ","))
{
var id = match.Groups[1].Value;
var value = match.Groups[2].Value;
retVal[int.Parse(id)] = value.Replace(",,", ",");
}
return retVal;
}
I think it can be adapted to the original question with an expression like @"([0-9]+),\s?"
and parse on Groups[0]
.
I hope it's helpful to somebody and thanks for the tips on getting it close to there, especially Asaph!
Upvotes: 1
Reputation: 361
i used this for a list of items that had to be alphanumeric without underscores at the front of each item.
^(([0-9a-zA-Z][0-9a-zA-Z_]*)([,][0-9a-zA-Z][0-9a-zA-Z_]*)*)$
Upvotes: 2
Reputation: 8170
Match duplicate comma-delimited items:
(?<=,|^)([^,]*)(,\1)+(?=,|$)
This regex can be used to split the values of a comma delimitted list. List elements may be quoted, unquoted or empty. Commas inside a pair of quotation marks are not matched.
,(?!(?<=(?:^|,)\s*"(?:[^"]|""|\\")*,)(?:[^"]|""|\\")*"\s*(?:,|$))
Upvotes: 9
Reputation: 24656
It depends a bit on your exact requirements. I'm assuming: all numbers, any length, numbers cannot have leading zeros nor contain commas or decimal points. individual numbers always separated by a comma then a space, and the last number does NOT have a comma and space after it. Any of these being wrong would simplify the solution.
([1-9][0-9]*,[ ])*[1-9][0-9]*
Here's how I built that mentally:
[0-9] any digit.
[1-9][0-9]* leading non-zero digit followed by any number of digits
[1-9][0-9]*, as above, followed by a comma
[1-9][0-9]*[ ] as above, followed by a space
([1-9][0-9]*[ ])* as above, repeated 0 or more times
([1-9][0-9]*[ ])*[1-9][0-9]* as above, with a final number that doesn't have a comma.
Upvotes: 13
Reputation: 12803
You might want to specify language just to be safe, but
(\d+, ?)+(\d+)?
ought to work
Upvotes: 1