Nathan Edwards
Nathan Edwards

Reputation: 13

regex to replace the 6th comma with a line break

I am trying to replace every 6th comma in the line of text with a line break. I have tried replacing ,{6} with \n in textpad with no luck. Any ideas?

Upvotes: 0

Views: 391

Answers (1)

h2ooooooo
h2ooooooo

Reputation: 39540

/^(([^\n,]+,){5}[^\n,]+),\s*/gm with replacement \1\n should work fine, dependent on your flavour.

/^((.*?,){5}.*?),\s*/gm also works fine, but you can't have the DOTALL modifier.

Input:

this, is, my, string, and, it, is, very, nice, and, pretty, cool
this, is, my, string, and, it, is, very, nice, and, pretty, cool
this, is, my, string, and, it, is, very, nice, and, pretty, cool
this, is, my, string, and, it, is, very, nice, and, pretty, cool
this, is, my, string, and, it, is, very, nice, and, pretty, cool¨

Output:

this, is, my, string, and, it
is, very, nice, and, pretty, cool
this, is, my, string, and, it
is, very, nice, and, pretty, cool
this, is, my, string, and, it
is, very, nice, and, pretty, cool
this, is, my, string, and, it
is, very, nice, and, pretty, cool
this, is, my, string, and, it
is, very, nice, and, pretty, cool

DEMO

Upvotes: 2

Related Questions