Regex for Comma delimited list

RegexCsv

Regex Problem Overview


What is the regular expression to validate a comma delimited list like this one:

12365, 45236, 458, 1, 99996332, ......

Regex Solutions


Solution 1 - Regex

I suggest you to do in the following way:

(\d+)(,\s*\d+)*

which would work for a list containing 1 or more elements.

Solution 2 - Regex

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.

Solution 3 - Regex

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.

Solution 4 - Regex

Match duplicate comma-delimited items:

(?<=,|^)([^,]*)(,\1)+(?=,|$)

Reference.

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*(?:,|$))

Reference.

Solution 5 - Regex

/^\d+(?:, ?\d+)*$/

Solution 6 - Regex

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_]*)*)$

Solution 7 - Regex

You might want to specify language just to be safe, but

(\d+, ?)+(\d+)?

ought to work

Solution 8 - Regex

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!

Solution 9 - Regex

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

Solution 10 - Regex

The following will match any comma delimited word/digit/space combination

(((.)*,)*)(.)*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneverLearningStudentView Question on Stackoverflow
Solution 1 - RegexAsaphView Answer on Stackoverflow
Solution 2 - RegexK. P.View Answer on Stackoverflow
Solution 3 - RegexmchermView Answer on Stackoverflow
Solution 4 - RegexmadcolorView Answer on Stackoverflow
Solution 5 - Regexw35l3yView Answer on Stackoverflow
Solution 6 - RegexPPPaulView Answer on Stackoverflow
Solution 7 - RegexDavid BergerView Answer on Stackoverflow
Solution 8 - Regexalpsystems.comView Answer on Stackoverflow
Solution 9 - Regexcrazy4groovyView Answer on Stackoverflow
Solution 10 - RegexAidanView Answer on Stackoverflow