- The first Regular Expression Library on the Web!

Please support RegExLib Sponsors


Regular Expression Details

Title Test Find Pattern Title
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.
1,,3""but, wait",5 | 1,2,3
"Test""a,b,c,d""ing" | no comma | semi; colon
Author Rating: The rating for this expression. Michael Ash
Your Rating
Bad Good

Enter New Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Work in PHP v4.3.9?
Name: Chris
Date: 4/14/2008 12:15:50 PM
I can't get this to work in PHP v4.3.9. I tried split() and preg_split(), but PHP gives me a 'lookbehind assertion is not fixed length at offset 50'.

Title: I have a version similar to yours
Name: Daniel Einspanjer
Date: 1/25/2006 2:18:53 PM
Nearly identical but mine allows commas or semicolons and it eats whitespace outside quotes. I gave you source credit on my entry since it is so similar to yours.

Title: Works very well
Name: Jonathan Roberts
Date: 1/14/2006 6:28:01 PM
Works very well once you realize it splits and does not match, now I can read in CSV files. Thanks for that

Title: Re:Non functioning?
Name: Michael Ash
Date: 9/11/2005 5:24:24 PM
The function of this regex is to split values as stated in the description, not to capture them. Values are to be obtained via the split method of the regex object, not the match. Matching only returns the separating commas.

Title: Non functioning?
Name: Jordy Boom
Date: 9/10/2005 7:04:25 PM
Test string: "test", "test" The match was succesful, but it did not capture anything.

Title: Updated
Name: Michael Ash
Date: 6/23/2004 3:13:34 PM
Changed from (?<=(?>(?:^|,)"(?:[^"]|""|\\")*?")),|(?<!(?>(?:^|,)"(?:[^"]|""|\\")*?")),(?!(?:[^"]|""|\\")*"(?:,|$)) to ,(?!(?<=(?:^|,)\s*"(?:[^"]|""|\\")*,)(?:[^"]|""|\\")*"\s*(?:,|$)) which seems to work better.

Title: Improvement...
Name: Michael Ash
Date: 3/9/2004 4:47:38 PM
I've replace this regex (?n:(?<!(^|,)"[^"]*),|,(?![^"]*"(,|$))) with this one (?<=(?>(?:^|,)"(?:[^"]|""|\\")*?")),|(?<!(?>(?:^|,)"(?:[^"]|""|\\")*?")),(?!(?:[^"]|""|\\")*"(?:,|$)) While this one isn't unbreakable either it does handle quoted strings a little better. Escaped double qoutes, two double quotes (""), within the quotes string will be handled in MOST cases. The most notable exception is when a comma immediately follows a double quote Unmatched quotes are no longer handled.

Title: Breakable
Name: Michael Ash
Date: 3/9/2004 1:26:07 AM
Yeah I saw how you could break this regex about an hour after i posted it. This looks like one of those 80-20 deals. It works well enough if the list elements stay simple, but if a quoted string contains another, well placed, quote it will break split usage. I've tried getting around it but finding out where the string would end also breaks the usablity of the split. Any suggestestion?

Title: Not sure about this
Name: Darren Neimke
Date: 3/8/2004 7:43:33 PM
Not sure if this is the correct usage but, when I tested the pattern like so: Dim p As String = "(?n:(?<!(^|,)""[^""]*),|,(?![^""]*""(,|$)))" Dim s As String = "This, is ,, a ""comma , , delimited"", list" Dim arr As String() = Regex.Split(s, p) For Each str As String In arr Debug.WriteLine(str) Next I received the following output which appears incorrect: This is a "comma delimited" list

Copyright © 2001-2014, | ASP.NET Tutorials