Regular expression for csv with commas and no quotes

痴心易碎 提交于 2019-12-25 07:47:01

问题


I'm trying to parse really complicated csv, which is generated wittout any quotes for columns with commas.
The only tip I get, that commas with whitespace before or after are included in field.

Jake,HomePC,Microsoft VS2010, Microsoft Office 2010

Should be parsed to

Jake
HomePC
Microsoft VS2010, Microsoft Office 2010

Can anybody advice please on how to include "\s," and ,"\s" to column body.


回答1:


If your language supports lookbehind assertions, split on

(?<!\s),(?!\s)

In C#:

string[] splitArray = Regex.Split(subjectString, 
    @"(?<!\s) # Assert that the previous character isn't whitespace
    ,         # Match a comma
    (?!\s)    # Assert that the following character isn't whitespace", 
    RegexOptions.IgnorePatternWhitespace);



回答2:


split by r"(?!\s+),(?!\s+)"

in python you can do this like

import re
re.split(r"(?!\s+),(?!\s+)", s) # s is your string



回答3:


Try this. It gave me the desired result which you have mentioned.

StringBuilder testt = new StringBuilder("Jake,HomePC,Microsoft VS2010, Microsoft Office 2010,Microsoft VS2010, Microsoft Office 2010");
Pattern varPattern = Pattern.compile("[a-z0-9],[a-z0-9]", Pattern.CASE_INSENSITIVE);
Matcher varMatcher = varPattern.matcher(testt);
List<String> list = new ArrayList<String>();
int startIndex = 0, endIndex = 0;
boolean found = false;
while (varMatcher.find()) {
endIndex = varMatcher.start()+1;
if (startIndex == 0) {
list.add(testt.substring(startIndex, endIndex));
} else {
startIndex++;
list.add(testt.substring(startIndex, endIndex));
}
startIndex = endIndex;
found = true;
}
if (found) {
if (startIndex == 0) {
list.add(testt.substring(startIndex));
} else {
list.add(testt.substring(startIndex + 1));
}
}
for (String s : list) {
System.out.println(s);
}

Please note that the code is in Java.



来源:https://stackoverflow.com/questions/11469655/regular-expression-for-csv-with-commas-and-no-quotes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!