0

I am trying to replace the every 4th comma of a string by new line.

In javascript below works good

 var str = "blue,red,green,orange,yellow,brown,black,teal,purple,gold,silver"
 str = str.replace(/(([^,]*,){4}([^,]*)),/g, '$1\n').

and gives me right output like below:

blue,red,green,orange,yellow
brown,black,teal,purple,gold
silver

I am trying to do the same in java with

String str = "blue,red,green,orange,yellow,brown,black,teal,purple,gold,silver";
str = str.replaceAll("([^,]*,){4}([^,]*)", "$1\n");

with output:
orange,
teal,
,gold,silver

Can anyone help me to get the right expression to replace nth(fourth) comma with new line in java

4
  • 2
    That's not valid java. It won't compile. Start with that and we'll see what we can do. Commented Sep 15, 2016 at 17:28
  • This Regexr reveals the pattern to be working as expected... Commented Sep 15, 2016 at 17:34
  • @ThisNameBetterBeAvailable - Thanks ,I tried this - (([^,]*,){4} - i see the comma appened in 1st and 2nd line which i dont want.Any clues how to remove that. Commented Sep 15, 2016 at 17:40
  • The capture buffer is being overwritten each quantified pass. ([^,]*,){4} so you only get the last one. What's wrong with using the same regex's? Commented Sep 15, 2016 at 17:47

1 Answer 1

1

In the JavaScript pattern, you have two selection groups that are nested in one selection group, $1 references the selection-group with the other two selection groups in it.

In the Java pattern, you have two selection-groups, and no other ones, $1 references to the first selection-group instead of the one that contains the other two as in the JavaScript pattern.

Removing the braces of the first selection-group in JavaScript reproduces the unexpected result, so adding braces around the Java pattern should solve your problem.

What went wrong

The first pattern's selection-group sls

Before

//JavaScript
var str = "blue,red,green,orange,yellow,brown,black,teal,purple,gold,silver"
str = str.replace(/(([^,]*,){4}([^,]*)),/g, '$1\n').
//Output:
//blue,red,green,orange,yellow
//,brown,black,teal,purple
//,gold,silver

//Java
String str = "blue,red,green,orange,yellow,brown,black,teal,purple,gold,silver";
str = str.replaceAll("([^,]*,){4}([^,]*)", "$1\n");
//Output:
//orange,
//teal,
//,gold,silver

Regexr for Java pattern (not fixed yet)

After

//JavaScript
var str = "blue,red,green,orange,yellow,brown,black,teal,purple,gold,silver"
str = str.replace(/(([^,]*,){4}([^,]*)),/g, '$1\n').
//Output:
//blue,red,green,orange,yellow
//,brown,black,teal,purple
//,gold,silver

//Java
String str = "blue,red,green,orange,yellow,brown,black,teal,purple,gold,silver";
str = str.replaceAll("(([^,]*,){4}([^,]*)),", "$1\n");
// These were added --^------------------^^
//Output:
//blue,red,green,orange,yellow
//,brown,black,teal,purple
//,gold,silver

Regexr for Java pattern (fixed)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks much for the explanation.(([^,]*,){4}).I tried this - (([^,]*,){4}) - i see the comma appened at the end in 1st and 2nd line which i dont want.Any clues how to remove that.
@Vinu Sorry, i missed the comma behind the first, working pattern. I updated my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.