【发布时间】:2015-02-18 15:19:40
【问题描述】:
我有一个字符串
5,(5,5),C'A,B','A,B',',B','A,',"A,B",C"A,B"
我想用逗号分割它,但需要在括号和引号中排除逗号(单引号和双引号)。
像这样
5(5,5)C'A,B''A,B'',B''A,'"A,B"C"A,B"
使用java正则表达式如何实现这个??
【问题讨论】:
我有一个字符串
5,(5,5),C'A,B','A,B',',B','A,',"A,B",C"A,B"
我想用逗号分割它,但需要在括号和引号中排除逗号(单引号和双引号)。
像这样
5(5,5)C'A,B''A,B'',B''A,'"A,B"C"A,B"
使用java正则表达式如何实现这个??
【问题讨论】:
你可以使用这个正则表达式:
String input = "5,(5,5),C'A,B','A,B',',B','A,',\"A,B\",C\"A,B\"";
String[] toks = input.split(
",(?=(([^']*'){2})*[^']*$)(?=(([^\"]*\"){2})*[^\"]*$)(?![^()]*\\))" );
for (String tok: toks)
System.out.printf("<%s>%n", tok);
输出:
<5>
<(5,5)>
<C'A,B'>
<'A,B'>
<',B'>
<'A,'>
<"A,B">
<C"A,B">
说明:
, # Match literal comma
(?=(([^']*'){2})*[^']*$) # Lookahead to ensure comma is followed by even number of '
(?=(([^"]*"){2})*[^"]*$) # Lookahead to ensure comma is followed by even number of "
(?![^()]*\\)) # Negative lookahead to ensure ) is not followed by matching
# all non [()] characters in between
【讨论】:
,(?![^(]*\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)
试试这个。
见demo。
对于java
,(?![^(]*\\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)
【讨论】:
,(?![^(]*\\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)
考虑匹配而不是splitting 字符串。
String s = "5,(5,5),C'A,B','A,B',',B','A,',\"A,B\",C\"A,B\"";
Pattern p = Pattern.compile("(?:[^,]*(['\"])[^'\"]*\\1|\\([^)]*\\))|[^,]+");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
输出
5
(5,5)
C'A,B'
'A,B'
',B'
'A,'
"A,B"
C"A,B"
【讨论】: