Notepad++ insert new line at every nth occurrence of a string/character
up vote
3
down vote
favorite
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++
add a comment |
up vote
3
down vote
favorite
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++
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 an
after every second,
.
– Wiktor Stribiżew
Dec 23 '16 at 10:13
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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++
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++
regex notepad++
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 an
after every second,
.
– Wiktor Stribiżew
Dec 23 '16 at 10:13
add a comment |
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 an
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
add a comment |
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}
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 withr
to insert a newline. And since Vim character classes do not match line breaks, there is no need to usenr
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
add a comment |
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}
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 withr
to insert a newline. And since Vim character classes do not match line breaks, there is no need to usenr
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
add a comment |
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}
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 withr
to insert a newline. And since Vim character classes do not match line breaks, there is no need to usenr
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
add a comment |
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}
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}
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 withr
to insert a newline. And since Vim character classes do not match line breaks, there is no need to usenr
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
add a comment |
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 withr
to insert a newline. And since Vim character classes do not match line breaks, there is no need to usenr
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 an
after every second,
.– Wiktor Stribiżew
Dec 23 '16 at 10:13