Notepad++ insert new line at every nth occurrence of a string/character











up vote
3
down vote

favorite
1












Using Notepad++ Find and Replace feature, I would like to insert a new line at every nth occurrence of a character or string (a comma in my case).



I have tried the regex below using "Regular expression" mode, but no luck.



Find what: ((,){1000})



Replace with: 1n










share|improve this question
























  • Would this work: (((.*?), ){1000})? I couldn't understand the regex.
    – Pugazh
    Dec 23 '16 at 10:03












  • The regex in your comment looks closer, have you tried it?
    – Tim Biegeleisen
    Dec 23 '16 at 10:08










  • It is not quite clear: every nth occurrence in the file or line?
    – Wiktor Stribiżew
    Dec 23 '16 at 10:08






  • 1




    If a line is meant, you may use (?:[^nr,]*,){2} (or your approach will also work - (?:.*?,){2}) -> $&n to insert a n after every second ,.
    – Wiktor Stribiżew
    Dec 23 '16 at 10:13

















up vote
3
down vote

favorite
1












Using Notepad++ Find and Replace feature, I would like to insert a new line at every nth occurrence of a character or string (a comma in my case).



I have tried the regex below using "Regular expression" mode, but no luck.



Find what: ((,){1000})



Replace with: 1n










share|improve this question
























  • Would this work: (((.*?), ){1000})? I couldn't understand the regex.
    – Pugazh
    Dec 23 '16 at 10:03












  • The regex in your comment looks closer, have you tried it?
    – Tim Biegeleisen
    Dec 23 '16 at 10:08










  • It is not quite clear: every nth occurrence in the file or line?
    – Wiktor Stribiżew
    Dec 23 '16 at 10:08






  • 1




    If a line is meant, you may use (?:[^nr,]*,){2} (or your approach will also work - (?:.*?,){2}) -> $&n to insert a n after every second ,.
    – Wiktor Stribiżew
    Dec 23 '16 at 10:13















up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





Using Notepad++ Find and Replace feature, I would like to insert a new line at every nth occurrence of a character or string (a comma in my case).



I have tried the regex below using "Regular expression" mode, but no luck.



Find what: ((,){1000})



Replace with: 1n










share|improve this question















Using Notepad++ Find and Replace feature, I would like to insert a new line at every nth occurrence of a character or string (a comma in my case).



I have tried the regex below using "Regular expression" mode, but no luck.



Find what: ((,){1000})



Replace with: 1n







regex notepad++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '16 at 8:37









Wiktor Stribiżew

300k16122197




300k16122197










asked Dec 23 '16 at 10:02









Pugazh

8,03841941




8,03841941












  • Would this work: (((.*?), ){1000})? I couldn't understand the regex.
    – Pugazh
    Dec 23 '16 at 10:03












  • The regex in your comment looks closer, have you tried it?
    – Tim Biegeleisen
    Dec 23 '16 at 10:08










  • It is not quite clear: every nth occurrence in the file or line?
    – Wiktor Stribiżew
    Dec 23 '16 at 10:08






  • 1




    If a line is meant, you may use (?:[^nr,]*,){2} (or your approach will also work - (?:.*?,){2}) -> $&n to insert a n after every second ,.
    – Wiktor Stribiżew
    Dec 23 '16 at 10:13




















  • Would this work: (((.*?), ){1000})? I couldn't understand the regex.
    – Pugazh
    Dec 23 '16 at 10:03












  • The regex in your comment looks closer, have you tried it?
    – Tim Biegeleisen
    Dec 23 '16 at 10:08










  • It is not quite clear: every nth occurrence in the file or line?
    – Wiktor Stribiżew
    Dec 23 '16 at 10:08






  • 1




    If a line is meant, you may use (?:[^nr,]*,){2} (or your approach will also work - (?:.*?,){2}) -> $&n to insert a n after every second ,.
    – Wiktor Stribiżew
    Dec 23 '16 at 10:13


















Would this work: (((.*?), ){1000})? I couldn't understand the regex.
– Pugazh
Dec 23 '16 at 10:03






Would this work: (((.*?), ){1000})? I couldn't understand the regex.
– Pugazh
Dec 23 '16 at 10:03














The regex in your comment looks closer, have you tried it?
– Tim Biegeleisen
Dec 23 '16 at 10:08




The regex in your comment looks closer, have you tried it?
– Tim Biegeleisen
Dec 23 '16 at 10:08












It is not quite clear: every nth occurrence in the file or line?
– Wiktor Stribiżew
Dec 23 '16 at 10:08




It is not quite clear: every nth occurrence in the file or line?
– Wiktor Stribiżew
Dec 23 '16 at 10:08




1




1




If a line is meant, you may use (?:[^nr,]*,){2} (or your approach will also work - (?:.*?,){2}) -> $&n to insert a n after every second ,.
– Wiktor Stribiżew
Dec 23 '16 at 10:13






If a line is meant, you may use (?:[^nr,]*,){2} (or your approach will also work - (?:.*?,){2}) -> $&n to insert a n after every second ,.
– Wiktor Stribiżew
Dec 23 '16 at 10:13














1 Answer
1






active

oldest

votes

















up vote
6
down vote



accepted










If you mean to add a newline after nth occurrence of any string on a line, I'd use



(?:.*?,){2}


and replace with $&n (or $&rn) where .*? matches any 0+ chars other than line break characters, as few as possible, up to the first occurrence of ,. The $& is the backreference to the whole match value (2 is used for the demo to look cleaner, 1000 is a rather big value). See a demo showing that a newline is placed after each second ,.



With a single char, you'd better use a negated character class (but add line break chars there to force the pattern to not overflow across lines):



(?:[^nr,]*,){2}


enter image description here






share|improve this answer

















  • 1




    Awesome, thanks!
    – Pugazh
    Dec 28 '16 at 8:35










  • How would you do this in vim?
    – Daniel van Flymen
    May 11 '17 at 19:16










  • @DanielvanFlymen: In Vim, you need to replace with r to insert a newline. And since Vim character classes do not match line breaks, there is no need to use nr in the [...]. And the backreference to the whole match is &. So, try :%s/([^,]*,){2}/&r/g.
    – Wiktor Stribiżew
    May 11 '17 at 19:25













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41299223%2fnotepad-insert-new-line-at-every-nth-occurrence-of-a-string-character%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote



accepted










If you mean to add a newline after nth occurrence of any string on a line, I'd use



(?:.*?,){2}


and replace with $&n (or $&rn) where .*? matches any 0+ chars other than line break characters, as few as possible, up to the first occurrence of ,. The $& is the backreference to the whole match value (2 is used for the demo to look cleaner, 1000 is a rather big value). See a demo showing that a newline is placed after each second ,.



With a single char, you'd better use a negated character class (but add line break chars there to force the pattern to not overflow across lines):



(?:[^nr,]*,){2}


enter image description here






share|improve this answer

















  • 1




    Awesome, thanks!
    – Pugazh
    Dec 28 '16 at 8:35










  • How would you do this in vim?
    – Daniel van Flymen
    May 11 '17 at 19:16










  • @DanielvanFlymen: In Vim, you need to replace with r to insert a newline. And since Vim character classes do not match line breaks, there is no need to use nr in the [...]. And the backreference to the whole match is &. So, try :%s/([^,]*,){2}/&r/g.
    – Wiktor Stribiżew
    May 11 '17 at 19:25

















up vote
6
down vote



accepted










If you mean to add a newline after nth occurrence of any string on a line, I'd use



(?:.*?,){2}


and replace with $&n (or $&rn) where .*? matches any 0+ chars other than line break characters, as few as possible, up to the first occurrence of ,. The $& is the backreference to the whole match value (2 is used for the demo to look cleaner, 1000 is a rather big value). See a demo showing that a newline is placed after each second ,.



With a single char, you'd better use a negated character class (but add line break chars there to force the pattern to not overflow across lines):



(?:[^nr,]*,){2}


enter image description here






share|improve this answer

















  • 1




    Awesome, thanks!
    – Pugazh
    Dec 28 '16 at 8:35










  • How would you do this in vim?
    – Daniel van Flymen
    May 11 '17 at 19:16










  • @DanielvanFlymen: In Vim, you need to replace with r to insert a newline. And since Vim character classes do not match line breaks, there is no need to use nr in the [...]. And the backreference to the whole match is &. So, try :%s/([^,]*,){2}/&r/g.
    – Wiktor Stribiżew
    May 11 '17 at 19:25















up vote
6
down vote



accepted







up vote
6
down vote



accepted






If you mean to add a newline after nth occurrence of any string on a line, I'd use



(?:.*?,){2}


and replace with $&n (or $&rn) where .*? matches any 0+ chars other than line break characters, as few as possible, up to the first occurrence of ,. The $& is the backreference to the whole match value (2 is used for the demo to look cleaner, 1000 is a rather big value). See a demo showing that a newline is placed after each second ,.



With a single char, you'd better use a negated character class (but add line break chars there to force the pattern to not overflow across lines):



(?:[^nr,]*,){2}


enter image description here






share|improve this answer












If you mean to add a newline after nth occurrence of any string on a line, I'd use



(?:.*?,){2}


and replace with $&n (or $&rn) where .*? matches any 0+ chars other than line break characters, as few as possible, up to the first occurrence of ,. The $& is the backreference to the whole match value (2 is used for the demo to look cleaner, 1000 is a rather big value). See a demo showing that a newline is placed after each second ,.



With a single char, you'd better use a negated character class (but add line break chars there to force the pattern to not overflow across lines):



(?:[^nr,]*,){2}


enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 23 '16 at 10:17









Wiktor Stribiżew

300k16122197




300k16122197








  • 1




    Awesome, thanks!
    – Pugazh
    Dec 28 '16 at 8:35










  • How would you do this in vim?
    – Daniel van Flymen
    May 11 '17 at 19:16










  • @DanielvanFlymen: In Vim, you need to replace with r to insert a newline. And since Vim character classes do not match line breaks, there is no need to use nr in the [...]. And the backreference to the whole match is &. So, try :%s/([^,]*,){2}/&r/g.
    – Wiktor Stribiżew
    May 11 '17 at 19:25
















  • 1




    Awesome, thanks!
    – Pugazh
    Dec 28 '16 at 8:35










  • How would you do this in vim?
    – Daniel van Flymen
    May 11 '17 at 19:16










  • @DanielvanFlymen: In Vim, you need to replace with r to insert a newline. And since Vim character classes do not match line breaks, there is no need to use nr in the [...]. And the backreference to the whole match is &. So, try :%s/([^,]*,){2}/&r/g.
    – Wiktor Stribiżew
    May 11 '17 at 19:25










1




1




Awesome, thanks!
– Pugazh
Dec 28 '16 at 8:35




Awesome, thanks!
– Pugazh
Dec 28 '16 at 8:35












How would you do this in vim?
– Daniel van Flymen
May 11 '17 at 19:16




How would you do this in vim?
– Daniel van Flymen
May 11 '17 at 19:16












@DanielvanFlymen: In Vim, you need to replace with r to insert a newline. And since Vim character classes do not match line breaks, there is no need to use nr in the [...]. And the backreference to the whole match is &. So, try :%s/([^,]*,){2}/&r/g.
– Wiktor Stribiżew
May 11 '17 at 19:25






@DanielvanFlymen: In Vim, you need to replace with r to insert a newline. And since Vim character classes do not match line breaks, there is no need to use nr in the [...]. And the backreference to the whole match is &. So, try :%s/([^,]*,){2}/&r/g.
– Wiktor Stribiżew
May 11 '17 at 19:25




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41299223%2fnotepad-insert-new-line-at-every-nth-occurrence-of-a-string-character%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff