Home >Java >javaTutorial >How to Split Comma-Separated Text While Ignoring Commas Within Quotes?
Splitting Comma-Separated Text Excluding Quotes
When processing comma-separated text with embedded quotes, it becomes necessary to ignore commas within the quoted sections. This issue arises when using the default string.split(",") method in Python.
Consider the following string:
"123,test,444,\"don't split, this\",more test,1"
Splitting this string using the default method would yield the following result:
["123", "test", "444", "\"don't split", " this\"", "more test", "1"]
As you can see, the comma inside the quoted section is not ignored. To address this, a regular expression is required.
str.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")
Explanation of the Regular Expression:
This regular expression ensures that only commas outside of quoted sections are recognized as separators.
Alternatively, using the (?x) modifier in Python allows you to break down the regular expression into multiple lines for improved readability:
str.split("(?x)\n\ , \n\ (?=\n\ (?: \n\ [^\"]* \n\ \" \n\ [^\"]* \n\ \" \n\ )* \n\ [^\"]* \n\ $ \n\ ) \n")
This code effectively splits the input string into a list where each element represents a substring separated by a comma outside of any quoted sections.
The above is the detailed content of How to Split Comma-Separated Text While Ignoring Commas Within Quotes?. For more information, please follow other related articles on the PHP Chinese website!