How to replace every x-th occurence of a string in a text with RegEx?

John

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.)

John

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do i Replace every n-th character x from an String

From Dev

bash function to replace every occurence of text in directory and subdirectories

From Dev

JS string replace only replacing every other occurence

From Dev

replace text in single line after specific occurence using regex

From Dev

How to replace a string on the 5th line of multiple text files?

From Dev

How to replace a string on the 5th line of multiple text files?

From Dev

How to replace string in a file text based on regex?

From Dev

Replace every character occurence but last

From Dev

Efficient/simple way to replace every occurrence but the last occurence of a substring in a string (str_replace_except_last)?

From Dev

How to replace text with regex?

From Dev

How to replace a text in regex?

From Dev

Replace every double quote in HTML Text with REGEX

From Dev

Replace every double quote in HTML Text with REGEX

From Dev

How to replace occurence of a character in a string except the first one

From Dev

Unable to replace String occurence in java?

From Dev

Replace particular occurence of a word in string

From Dev

Replace every occurence of characters by another characters

From Dev

Find and replace every second occurence with sed

From Dev

Oracle regexp_replace every occurence

From Dev

php preg_replace every second occurence

From Dev

Replace every other occurence on the same line

From Dev

Find and replace every second occurence with sed

From Dev

RegEx to match last occurence of a string

From Dev

Python extract occurence of a string with regex

From Dev

Awk one-liner to replace text of first matching regex occurence only

From Dev

How to replace last occurence of a character?

From Dev

How to replace the n th occurance of a character in a String ?

From Dev

How to replace text in a string

From Dev

Replace second occurence of a substring using regex

Related Related

  1. 1

    How do i Replace every n-th character x from an String

  2. 2

    bash function to replace every occurence of text in directory and subdirectories

  3. 3

    JS string replace only replacing every other occurence

  4. 4

    replace text in single line after specific occurence using regex

  5. 5

    How to replace a string on the 5th line of multiple text files?

  6. 6

    How to replace a string on the 5th line of multiple text files?

  7. 7

    How to replace string in a file text based on regex?

  8. 8

    Replace every character occurence but last

  9. 9

    Efficient/simple way to replace every occurrence but the last occurence of a substring in a string (str_replace_except_last)?

  10. 10

    How to replace text with regex?

  11. 11

    How to replace a text in regex?

  12. 12

    Replace every double quote in HTML Text with REGEX

  13. 13

    Replace every double quote in HTML Text with REGEX

  14. 14

    How to replace occurence of a character in a string except the first one

  15. 15

    Unable to replace String occurence in java?

  16. 16

    Replace particular occurence of a word in string

  17. 17

    Replace every occurence of characters by another characters

  18. 18

    Find and replace every second occurence with sed

  19. 19

    Oracle regexp_replace every occurence

  20. 20

    php preg_replace every second occurence

  21. 21

    Replace every other occurence on the same line

  22. 22

    Find and replace every second occurence with sed

  23. 23

    RegEx to match last occurence of a string

  24. 24

    Python extract occurence of a string with regex

  25. 25

    Awk one-liner to replace text of first matching regex occurence only

  26. 26

    How to replace last occurence of a character?

  27. 27

    How to replace the n th occurance of a character in a String ?

  28. 28

    How to replace text in a string

  29. 29

    Replace second occurence of a substring using regex

HotTag

Archive