A sample text is
"abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "
where abc
could be every string of any length (.*
) except the 4-char-string ", "
.
For my test case where
the search needle is the four chars ", "
and every 4th occurence should be replaced
I came up with
(([^(", ")]*", "[^(", ")]*){3})", "
and
((.*", ".*){3})", "
Those both do not work in notepad++ when using it's regex search. (I just see I didn't try them in the browser scratchpad. But I suspect the error anyway in my regex-term.)
For replacement I tried $1", \n"
.
What's the right regex and the right replacement?
Bonus question :)
How to achieve, that the "
of the search needle will be used for the count of the next 4 occurences of the needle?
Maybe another description:
addLineBreaksToLongString(a="text",b="-, -",k=3,d=3) {
// a the String, b signal string to break at, k number of letters of b to keep on old line, d only break at every d-th occurence of b
(I have written a javascript function to achieve this. So no need to put time in posting one. I want to improve my regex writing, because it's much faster to use in single cases.)
So finally this works:
((.*?", "){3}.*?)", "
and as replacement
$1", \n"
This works in notepad++, too.
To match new-line characters, too, instead of .*
use [\S\s]*
:
(([\S\s]*?", "){3}[\S\s]*?)", "
as .*
possibly doesn't match new-line characters, depending on the regex engine used.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments