我有一根绳子
5,(5,5),C'A,B','A,B',',B','A,',"A,B",C"A,B"
Run Code Online (Sandbox Code Playgroud)
我想用逗号分割它,但需要排除括号和引号内的逗号(单引号和双引号)。
像这样
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);
Run Code Online (Sandbox Code Playgroud)
输出:
<5>
<(5,5)>
<C'A,B'>
<'A,B'>
<',B'>
<'A,'>
<"A,B">
<C"A,B">
Run Code Online (Sandbox Code Playgroud)
解释:
, # 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
Run Code Online (Sandbox Code Playgroud)