Replace but the last occurrence of a string in the text

Suppose I have this piece of text:

Saturday and Sunday and Monday and Tuesday and Wednesday and Thursday and Friday are days of the week. 

I want all but the last and to be replaced by a comma:

 Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday and Friday are days of the week. 

Is there an easy way to do this in regex? As far as I know, the replace method in a regular expression replaces strings completely.

+5
source share
2 answers

str.replace() method has an argument count :

str.replace(old, new[, count])

Returns a copy of the string with all occurrences of the substring old, replaced by the new one. If an optional argument parameter is specified, only the first counter instances are replaced.

Then use str.count() to check the number of and in the string, and then -1 (because you need the last and ):

str.count(sub[, start[, end]])

Returns the number of non-overlapping occurrences of the substring sub in the range [start, end] . The optional start and end arguments are interpreted as in slice notation.

Demo:

 >>> string = 'Saturday and Sunday and Monday and Tuesday and Wednesday and Thursday and Friday are days of the week.' >>> string.replace(' and ', ", ", (string.count(' and ')-1)) 'Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday and Friday are days of the week. ' 
+15
source

If you want to use the regex, you can match all and followed by another line in the next line.

 >>> str='Monday and Tuesday and Wednesday and Thursday and Friday and Saturday and Sunday are the days of the week.' >>> import re >>> re.sub(' and (?=.* and )', ', ', str) 'Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday are the days of the week.' 

(?= ... ) is a lookahead that guarantees that there will be a match in the string, not including it in the actual match (also not in the substitution). It looks like a conditional match.

+4
source

Source: https://habr.com/ru/post/1235853/


All Articles