List of regular expressions for commas

What is a regular expression for checking a comma separated list like this:

12365, 45236, 458, 1, 99996332, ...... 
+63
regex csv
Sep 08 '09 at 20:12
source share
11 answers

I suggest you do the following:

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

which will work for a list containing 1 or more items.

+102
Sep 08 '09 at 20:40
source share

This regular expression extracts an item from a comma-separated list, regardless of content:

 (.+?)(?:,|$) 

If you just replace the comma with something else, it should work for any separator.

+11
Aug 03 '15 at 13:53 on
source share

It depends on your exact requirements. I assume: all numbers, any length, numbers cannot have leading zeros and do not contain commas or decimal points. individual numbers are always separated by a comma and then a space, and the last number does not have a comma and a space after it. Any of these wrong decisions will simplify the decision.

  ([1-9] [0-9] *, []) * [1-9] [0-9] * 

This is how I mentally built it:

 [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. 
+9
Sep 08 '09 at 20:16
source share

Combine duplicate comma separated items:

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

Link

This regular expression can be used to separate comma-separated list values. List items may be specified incorrectly or empty. Commas within a pair of quotation marks do not match.

 ,(?!(?<=(?:^|,)\s*"(?:[^"]|""|\\")*,)(?:[^"]|""|\\")*"\s*(?:,|$)) 

Link

+7
Sep 08 '09 at 20:14
source share
 /^\d+(?:, ?\d+)*$/ 
+5
Sep 08 '09 at 20:18
source share

I used this to list items that were supposed to be alphanumeric without an underscore at the beginning of each item.

 ^(([0-9a-zA-Z][0-9a-zA-Z_]*)([,][0-9a-zA-Z][0-9a-zA-Z_]*)*)$ 
+2
Nov 08 '11 at 20:27
source share

This will reject extraneous commas at the beginning or end of the line if this is important to you.

 ((, )?(^)?(possible|value|patterns))* 

Replace possible|value|patterns regular expression that matches your allowed values.

+2
Nov 22 '13 at 2:10
source share

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

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

must work

+1
Sep 08 '09 at 20:15
source share

I had a slightly different requirement: for parsing an encoded dictionary / hash table with escaped commas, for example:

 "1=This is something, 2=This is something,,with an escaped comma, 3=This is something else" 

I think this is a nifty solution with a trick that avoids the complexity of regex:

 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 into Groups[0] .

I hope this helps someone and thanks for the tips on how to approach him, especially Asaf!

+1
Jun 06 2018-12-06T00:
source share

In JavaScript, use split to help and catch any negative numbers:

 '-1,2,-3'.match(/(-?\d+)(,\s*-?\d+)*/)[0].split(','); // ["-1", "2", "-3"] // may need trimming if digits are space-separated 
+1
Nov 03 '15 at 15:54
source share

The following will match any combination of words / numbers / spaces separated by commas

 (((.)*,)*)(.)* 
+1
Aug 13 '19 at 8:58
source share



All Articles