import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = ",?\\s?(\"[^\"]*,[^\"]*\"|[^\",]*)";
final String string = "St. Lawrence County,NY,,2,,Attainment Plan/Maintenance Plan,37,,1852,Nonattainment,,,,11922,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,2,,5,,0,0,,,,,,,,,,,,,,11922,,,,St. Lawrence County,4700,NY,NY,SO2.2010.St_Lawrence_Co,SO2.2010.St_Lawrence_Co.NY,,,,,,,,,,\n"
+ "Howard County,TX,,6,,Attainment Plan/Maintenance Plan,37,,1853,Nonattainment,,,,281,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,6,,5,,0,0,,,,,,,,,,,,,,281,,,,Howard County,4710,TX,TX,SO2.2010.Howard_Co,SO2.2010.Howard_Co.TX,,,,,,,,,,\n"
+ "Hutchinson County,TX,,6,,Attainment Plan/Maintenance Plan,37,,1854,Nonattainment,,,,14593,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,6,,5,,0,0,,,,,,,,,,,,,,14593,,,,Hutchinson County,4720,TX,TX,SO2.2010.Hutchinson_Co,SO2.2010.Hutchinson_Co.TX,,,,,,,,,,\n"
+ "Navarro County,TX,,6,,Attainment Plan/Maintenance Plan,37,,1855,Nonattainment,,,,1593,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,6,,5,,0,0,,,,,,,,,,,,,,1593,,,,Navarro County,4730,TX,TX,SO2.2010.Navarro_Co,SO2.2010.Navarro_Co.TX,,,,,,,,,,\n"
+ "Giles County,VA,,3,,Attainment Plan/Maintenance Plan,37,,1856,Nonattainment,,,,183,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,3,,5,,0,0,,,,,,,,,,,,,,183,,,,Giles County,4740,VA,VA,SO2.2010.Giles_Co,SO2.2010.Giles_Co.VA,,,,,,,,,,\n"
+ "New Madrid County,MO,,7,,Attainment Plan/Maintenance Plan,37,,1851,Nonattainment,,,,2,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,7,,5,,0,0,,,,,,,,,,,,,,2,,,,New Madrid County,4690,MO,MO,SO2.2010.New_Madrid_Co,SO2.2010.New_Madrid_Co.MO,,,,,,,,,,\n"
+ "Henderson-Webster Counties,KY,,4,,Attainment Plan/Maintenance Plan,37,,1850,Nonattainment,,,,6932,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,4,,5,,0,0,,,,,,,,,,,,,,6932,,,,Henderson-Webster Counties,4680,KY,KY,SO2.2010.Henderson_Webster_Cos,SO2.2010.Henderson_Webster_Cos.KY,,,,,,,,,,\n"
+ "Whatcom County,WA,,10,,Attainment Plan/Maintenance Plan,37,,1857,Nonattainment,,,,32,,,4/30/2021,4/30/2026,ppm,,,,,,,,,,,,,,,,,,,10,,5,,0,0,,,,,,,,,,,,,,32,,,,Whatcom County,4750,WA,WA,SO2.2010.Whatcom_Co,SO2.2010.Whatcom_Co.WA,,,,,,,,,,";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html