Replace nth occurrence of substring in string
I use simple function, which lists all occurrences, picks the nth one's position and uses it to split original string into two substrings. Then it replaces first occurrence in the second substring and joins substrings back into the new string:
import re
def replacenth(string, sub, wanted, n):
where = [m.start() for m in re.finditer(sub, string)][n-1]
before = string[:where]
after = string[where:]
after = after.replace(sub, wanted, 1)
newString = before + after
print(newString)
For these variables:
string = 'ababababababababab'
sub = 'ab'
wanted = 'CD'
n = 5
outputs:
ababababCDabababab
Notes:
The
where
variable actually is a list of matches' positions, where you pick up the nth one. But list item index starts with0
usually, not with1
. Therefore there is an-1
index andn
variable is the actual nth substring. My example finds 5th string. If you usen
index and want to find 5th position, you'll needn
to be4
. Which you use usually depends on the function, which generates ourn
.
This should be the simplest way, but maybe it isn't the most Pythonic way, because the
where
variable construction needs importingre
library. Maybe somebody will find even more Pythonic way.
Sources and some links in addition:
where
construction: How to find all occurrences of a substring?- string splitting: https://www.daniweb.com/programming/software-development/threads/452362/replace-nth-occurrence-of-any-sub-string-in-a-string
- similar question: Find the nth occurrence of substring in a string
Python - replace every nth occurrence of string
The code you got from the previous question is a nice starting point, and only a minimal adaptation is required to have it change every nth occurence:
def nth_repl_all(s, sub, repl, nth):
find = s.find(sub)
# loop util we find no match
i = 1
while find != -1:
# if i is equal to nth we found nth matches so replace
if i == nth:
s = s[:find]+repl+s[find + len(sub):]
i = 0
# find + len(sub) + 1 means we start after the last match
find = s.find(sub, find + len(sub) + 1)
i += 1
return s
Javascript: Replace nth occurrence of substring in multiline string
You regex doesn't match properly. .
will not match newline characters so what I'll do is (?:.|\n)
instead.
var someString="I have a cat, my cat is intelligent,\n my cat is very active";
someString= someString.replace(new RegExp("(?:(?:.|\n)*?my cat){2}"), function(x){return x.replace(RegExp("my cat$"), 'our cat abc')});
console.log(someString);
How to replace the Nth occurrence of a word in a string?
s = 'I have a banana and a banana and another banana'
word = 'banana'
replace_with = 'apple'
occurrance = 2
word.join(s.split(word)[:occurrance]) + replace_with + word.join(s.split(word)[occurrance:])
'I have a banana and a apple and another banana'
If you just want to process the last occurance
then make occurrance = -1
Find nth occurrence of a character in a row and replace it in Python, in order to fix/replace a wrong/leftover ; separator in a text file
You should not replace that semi-colon with another character, but instead wrap the string with double quotes (and escape any double quotes that might already be part of the string by doubling them). This is how in CSV syntax you can include such delimiters. In fact, it is not bad practice to wrap such text with double quotes always.
Here is how I'd adapt your loop:
for row in listoflines:
if row.count(';') > 13: # maybe there are even two or more of them
cells = row.split(';')
# Escape double quotes and wrap in double quotes
s = '"' + ';'.join(cells[12:-1]).replace('"', '""') + '"'
# Replace the involved parts with that single part
cells[12:-1] = [s]
# ...and convert back to the string format
row = ';'.join(cells)
finaldocument.append(row)
Replace Nth occurrence of a character in a string with something else
You can replace ((?:\d+, ){3}\d),
with \1\n
You basically capture everything till fourth comma in group1 and comma separately and replace it with \1\n
which replaces matched text with group1 text and newline, giving you the intended results.
Regex Demo
R Code demo
gsub("((?:\\d+, ){3}\\d),", "\\1\n", "1, 2, 3, 4, 5, 6, 7, 8, 9, 10")
Prints,
[1] "1, 2, 3, 4\n 5, 6, 7, 8\n 9, 10"
Edit:
To generalize above solution to any text, we can change \d
to [^,]
New R code demo
gsub("((?:[^,]+, ){3}[^,]+),", "\\1\n", "1, 2, 3, 4, 5, 6, 7, 8, 9, 10")
gsub("((?:[^,]+, ){3}[^,]+),", "\\1\n", "a, bb, ccc, dddd, 500, 600, 700, 800, 900, 1000")
Output,
[1] "1, 2, 3, 4\n 5, 6, 7, 8\n 9, 10"
[1] "a, bb, ccc, dddd\n 500, 600, 700, 800\n 900, 1000"
Replace String in between nth and n+1 occurrence of a character in Java
The better way - using regex
String input = "abc_1234_01233456_DC";
Matcher matcher = Pattern.compile("(_([^_]+)){2}").matcher(input);//{2} - number of occurrence
String result = matcher.find() ? new StringBuilder(input).replace(matcher.start(2), matcher.end(2), "78910").toString() : input; //abc_1234_78910_DC
That's it
Related Topics
How to Write Data into CSV Format as String (Not File)
Fill Between Two Vertical Lines in Matplotlib
Datetime Timezone Conversion Using Pytz
Overloaded Functions in Python
Compile Main Python Program Using Cython
Pandas Finding Local Max and Min
How to Convert an Rgb Image to Numpy Array
Why Are Python Strings and Tuples Are Made Immutable
How to Concatenate Two Dataframes Without Duplicates
Hitting Maximum Recursion Depth Using Pickle/Cpickle
How to Set the Default Color Cycle for All Subplots with Matplotlib
Difference Between Type(Obj) and Obj._Class_
Python 2.X - Write Binary Output to Stdout
How to Call Python Functions Dynamically
Running an Interactive Command from Within Python
Python: One Try Multiple Except