4

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"

Using java Regular Expression how to achieve this ??

Anupam Maiti
  • 1,570
  • 1
  • 19
  • 35

3 Answers3

5

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
anubhava
  • 761,203
  • 64
  • 569
  • 643
2
,(?![^(]*\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)

Try this.

See demo.

For java

,(?![^(]*\\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)
vks
  • 67,027
  • 10
  • 91
  • 124
2

Instead of splitting the string, consider matching instead.

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"
hwnd
  • 69,796
  • 4
  • 95
  • 132