Ten Thousand,10000, 2710 ,,"10,000","It's ""10 Grand"", baby",10K # Either some non-quote/non-comma text . . . [^",]+ # . . . or . . . | # . . . a double-quoted field (inside, paired double quotes are allowed) " # field's opening quote (?: [^"] | "" )* " # field's closing quote # Either some non-quote/non-comma text . . . ( [^",]+ ) # . . . or . . . | # . .. a double-quoted field (inside, paired double quotes are allowed) " # field's opening quote ( (?: [^"] | "" )* ) " # field's closing quote while ($line =~ m{ # Either some non-quote/non-comma text . . . ( [^",]+ ) # . . . or . . . | # . . . a double-quoted field ("" allowed inside) " # field's opening quote ( (?: [^"] | "" )* ) " # field's closing quote }gx) { if (defined $1) { $field = $1; } else { $field = $2; $field =~ s/""/"/g; } print "[$field]"; # print the field, for debugging Can work with $field now . . . } [Ten Thousand][10000][ 2710 ][10,000][It's "10 Grand", baby][10K] (?:^|,) (?: # Either some non-quote/non-comma text.... ( [^",]* ) # ... or... | # ... a double-quoted field (inside, paired double quotes are allowed) " # field's opening quote ( (?: [^"] | "" )* ) " # field's closing quote ) (?:^|,) (?: # Now, match either a double-quoted field (inside, paired double quotes are allowed) . . . " # (double-quoted field's opening quote) ( (?: [^"] | "" )* ) " # (double-quoted field's closing quote) | # . . . or, some non-quote/non-comma text . . . ( [^",]* ) ) import java.util.regex.*; Pattern fieldRegex = Pattern.compile( "\\G(?:^|,) \n"+ "(?: \n"+ " # Either a double-quoted field ... \n"+ " \" # field's opening quote \n"+ " ( (?: [^\"]++ | \"\" )*+ ) \n"+ " \" # field's closing quote \n"+ " # ... or ... \n"+ " | \n"+ " # ... some non-quote/non-comma text ... \n"+ " ( [^\",]* ) \n"+ " ) \n", Pattern.COMMENTS); Pattern quotesRegex = Pattern.compile("\"\""); // Given the string in 'line', find all the fields . . . Matcher m = fieldRegex.matcher(line); while (m.find()) { String field; if (m.group(1) != null) { field = quotesRegex.matcher(m.group(1)).replaceAll("\""); } else { field = m.group(2); } // We can now work with the field . . . System.out.println("[" + field + "]"); } Imports System.Text.RegularExpressions Dim FieldRegex as Regex = New Regex( _ "(?:^|,) " & _ "(?: " & _ " (?# Either a doublequoted field ...) " & _ " "" (?# field's opening quote ) " & _ " ( (?> [^""]+ | """" )* ) " & _ " "" (?# field's closing quote ) " & _ " (?# ... or ...) " & _ " | " & _ " (?# ... some non-quote/non-comma text ...) " & _ " ( [^"",]* ) " & _ " )", RegexOptions.IgnorePatternWhitespace) Dim QuotesRegex as Regex = New Regex("""""") 'A string with two double quotes Dim FieldMatch as Match = FieldRegex.Match(Line) While FieldMatch.Success Dim Field as String If FieldMatch.Groups(1).Success Field = QuotesRegex.Replace(FieldMatch.Groups(1).Value, """") Else Field = FieldMatch.Groups(2).Value End If Console.WriteLine("[" & Field & "]") ' Can now work with 'Field'.... FieldMatch = FieldMatch.NextMatch End While ----------------------------------------------------------------------------- Copyright 1997-2025 Jeffrey Friedl