简体   繁体   English

正则表达式:逗号分割,但在括号和引号中排除逗号(单双和双)

[英]Regex : Split on comma , but exclude commas within parentheses and quotes(Both single & Double)

I have one string 我有一个字符串

5,(5,5),C'A,B','A,B',',B','A,',"A,B",C"A,B" 

I want to split it on comma but need to exclude commas within parentheses and quotes(Both single and double quotes). 我想在逗号上拆分它,但需要在括号和引号(单引号和双引号)中排除逗号。

Like this 像这样

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"

Using java Regular Expression how to achieve this ?? 用java Regular Expression如何实现这个?

You can use this regex: 你可以使用这个正则表达式:

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);

Output: 输出:

<5>
<(5,5)>
<C'A,B'>
<'A,B'>
<',B'>
<'A,'>
<"A,B">
<C"A,B">

Explanation: 说明:

,                         # 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
,(?![^(]*\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)

Try this. 尝试这个。

See demo . 演示

For java 对于java

,(?![^(]*\\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)

Instead of split ting the string, consider matching instead. 而不是split字符串,而是考虑匹配。

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());
}

Output 产量

5
(5,5)
C'A,B'
'A,B'
',B'
'A,'
"A,B" 
C"A,B"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Regex - 拆分逗号分隔列表,但在括号内排除逗号 - Java Regex - split comma separated list, but exclude commas within parentheses 逗号分隔的字符串排除双引号中的逗号并分隔相邻的逗号 - String split on comma exclude comma in double quote and split adjacent commas Java Regex - 拆分逗号分隔列表但排除方括号内的逗号 - Java Regex - Split comma separated list but exclude commas within square brackets 用引号和引号内的逗号分隔逗号分隔的字符串,并在引号内使用转义引号 - Split comma separated string with quotes and commas within quotes and escaped quotes within quotes Java正则表达式:拆分逗号分隔的值,但忽略引号中的逗号 - Java regex: split comma-separated values but ignore commas in quotes 在逗号上拆分一个不带双引号的逗号的字符串 - Split a string on commas not contained within double-quotes with a twist 正则表达式替换不在引号内的字符串(单引号或双引号) - Regex to replace a string not within quotes (single or double) 用逗号分隔字符串但忽略括号或引号中的逗号 - Split string by comma but ignore commas in brackets or in quotes Stream Api - 用逗号分割排除嵌套逗号 - Stream Api - split by comma exclude nested commas Java正则表达式在空格上分割,不能在单引号或双引号之前或之后 - Java regex split on whitespace not preceded or followed by single or double quotes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM