【发布时间】:2019-10-17 15:54:14
【问题描述】:
在这里我试图在double quotes 中找到String。
List<String> getList(String value){
String regex = "\"[^\"]*\"|[^,]+";
List<String> allMatches = new ArrayList<String>();
if (StringUtils.isNotBlank(value)) {
Matcher m = Pattern.compile(regex).matcher(value);
while (m.find() && StringUtils.isNotBlank(m.group())) {
String str=m.group().replaceAll("^\"|\"$", "");
allMatches.add(str.trim());
}
}
return allMatches;
}
result = getList(400,test,\"don't split, this\",15);
result have [400,test,don't split, this,15] all comma seperated string except inside quotes.
它适用于模式 "" 但不适用于 “” 。 "foo,bar",不同于"foo,bar" here is not working regex
【问题讨论】:
-
将第一个和第二个
\"替换为[\"“],将最后一个\"替换为[\"”] -
@PaulLemarchand 这会起作用,但它也会匹配混合引号,例如
"HELLO”. -
可能是因为这个引号是不同的字符:" U+0022, ” U+201C, ” U+201D。
-
@TimBiegeleisen 我相信如果他想在两个引号之间查找字符串,
"...”符合这个要求。
标签: java regex regex-lookarounds regex-negation regex-greedy