RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
\s*[;,]\s*(?!(?<=(?:^|[;,])\s*"(?:[^"]|""|\\")*[;,]\s*)(?:[^"]|""|\\")*"\s*(?:[;,]|$))
Description
This regex splits comma or semicolon separated lists of optionally quoted strings. It handles quoted delimiters and escaped quotes. Whitespace inside quotes is preserved, outside is eaten.
Matches
a,b, c, d , A;B; C; D ; "e","f", "g", "h" , "E";" F"; "G "; " H " ; "i,i", "j;j"; "k,k, k ,k , k"; "l;l; l ;l ; l", "m,m,", "n,n;;"; "o\",\"o,o", "p"",""p""", q""q, r\"r; " s ", "t""\",t";"u\""";u", "v"v"v", ""w""w""; x\"x, "y,y"; z
Non-Matches
"chars,trailing,after,quotes,break;the"regex
Author Rating: Not yet rated. Daniel Einspanjer
Source Michael Ash - http://www.regxlib.com/REDetails.aspx?regexp_id=621
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
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: Nice job, thx
Name: Alessandro Di Noia
Date: 11/7/2006 5:01:53 AM
Comment:
Works great! nice job thx again


Title: New test case
Name: Daniel Einspanjer
Date: 1/25/2006 5:14:21 PM
Comment:
a,b, c, d , A;B; C; D ; "e","f", "g", "h" , "E";" F"; "G "; " H " ; "i,i", "j;j"; "k,k, k ,k , k"; "l;l; l ;l ; l", "m,m,", "n,n;;"; "o\",\"o,o", "p"",""p""", q""q, r\"r; " s ", "t""\",t";"u\""";u", "v"v"v", ""w""w""; x\"x, "y,y"; z; "chars,trailing,after,quotes,break;the"regex


Title: Here is an updated test case for the fixed regex
Name: Daniel Einspanjer
Date: 1/25/2006 5:13:22 PM
Comment:
a,b, c, d , A;B; C; D ; "e","f", "g", "h" , "E";" F"; "G "; " H " ; "i,i", "j;j"; "k,k, k ,k , k"; "l;l; l ;l ; l", "m,m,", "n,n;;"; "o\",\"o,o", "p"",""p""", q""q, r\"r; " s ", "t""\",t";"u\""";u", "v"v"v", ""w""w""; x\"x, "y,y"; z; "chars,trailing,after,quotes,break;the"regex


Title: Nice catch!
Name: Daniel Einspanjer
Date: 1/25/2006 5:09:03 PM
Comment:
I wish I could correct the regex but the site crashes whenever I click edit. (something went wrong with the sample matches when I was putting them in). The fix is another whitespace capture in the negative lookahead: \s*[;,]\s*(?!(?<=(?:^|[;,])\s*"(?:[^"]|""|\\")*[;,]\s*)(?:[^"]|""|\\")*"\s*(?:[;,]|$))


Title: Commas within the quotes
Name: Michael Ash
Date: 1/25/2006 3:53:43 PM
Comment:
If a comma inside quotes is followed by a whitespace it will match even though it shouldn't. Tested with A,b,123,"split, four"


Title: Great, the website is choking over the sample matches
Name: Daniel Einspanjer
Date: 1/25/2006 2:16:51 PM
Comment:
Here is an example of the different types of elements it can split: a,b, c, d , A;B; C; D ; "e","f", "g", "h" , "E";" F"; "G "; " H " ; "i,i", "j;j"; "k,k"; "l;l", "m,m,", "n,n;;"; "o\",\"o,o", "p"",""p""", q""q, r\"r; " s ", "t""\",t";"u\""";u", "v"v"v", ""w""w""; x\"x, "y,y"; z; "chars,trailing,after,quotes,break;the"regex


Copyright © 2001-2018, RegexAdvice.com | ASP.NET Tutorials