-1

Consider i have a String like this :

String str = "a,b,c,d," + char1 + "e,f" + char2 + ",g,h,i,j";

How to split every thing with , avoid every thing between char1 and char2 that not ,.

I can use this regex ,(?![^()]*\\)) to split only if char1 = '(' and char2 = ')' like this :

char char1 = '(', char2 = ')';
String str = "a,b,c,d," + char1 + "e,f,g" + char2 + ",h";
String s2[] = str.toString().split(",(?![^()]*\\))");

I get this result :

a
b
c
d
e,f,g
h

So how to generalize this to use any char in char1 and char2.

Thank you.

YCF_L
  • 51,266
  • 13
  • 85
  • 129
  • What do you mean by "any char"? Can char1 and/or char2 be a ',' (comma) ? – Betlista Feb 03 '17 at 11:24
  • 1
    This question looks familiar... look [here](http://stackoverflow.com/questions/42021220/how-to-split-string-a-b-c-d-e-f-g-h) – QBrute Feb 03 '17 at 11:25
  • @Betlista nope sorry can't be a `,` i forgot this part i will edit my question – YCF_L Feb 03 '17 at 11:25
  • Is it possible to split the strings before they are concatenated? And if not, are you able to control what char1 and char2 are before concatenation? That way you could enter a wildcard in both so you could focus the regex on that wildcard. – Titulum Feb 03 '17 at 11:26
  • Use `"(?:" +Pattern.quote(char1) + ".*?" + Pattern.quote(char2) + "|[^,])+"` to *match* the tokens. – Wiktor Stribiżew Feb 03 '17 at 11:28
  • yes @QBrute it is similar i want to generalize it – YCF_L Feb 03 '17 at 11:28

1 Answers1

1

Use a matching approach: match any substring between the chars you define and then any character not equal to a , and the user-defined chars.

char char1 = '|', char2 = ')';
String str = "a,b,c,d," + char1 + "e,f,g" + char2 + ",h";
String ch1_quoted = Pattern.quote(Character.toString(char1));
String ch2_quoted = Pattern.quote(Character.toString(char2));
List<String> s2 = new ArrayList<>();
Pattern pattern = Pattern.compile(ch1_quoted + "(.*?)" 
                                    + ch2_quoted + "|[^," + ch1_quoted + ch2_quoted + "]+", Pattern.DOTALL);
Matcher matcher = pattern.matcher(str);
while (matcher.find()){
    if (matcher.group(1) != null) {
        s2.add(matcher.group(1));
        System.out.println(matcher.group(1));
    } else {
        s2.add(matcher.group(0)); 
        System.out.println(matcher.group(0));
    }
} 

See the Java demo.

The Pattern.DOTALL is used to make . match line break characters - just in case.

See the sample code regex demo.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
  • Sorry, can you improve your pattern to remove the two characters in result so instead to `|e,f,g)` the result should be without this two chars like so `e,f,g` – YCF_L Feb 03 '17 at 12:43
  • i think it is my mistake in question i put `a b c d (e,f,g) h` instead to `b c d e,f,g h` – YCF_L Feb 03 '17 at 12:45
  • See http://ideone.com/yWoUsa. Not sure it will work as expected as I do not know what your data looks like. – Wiktor Stribiżew Feb 03 '17 at 13:02
  • ooohh this is it it work perfectly you are good thank you you can edit your answer i already changes my question – YCF_L Feb 03 '17 at 13:04