Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To replace but the last occurrence of string in a text [duplicate]

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 with a comma:

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

Is there an easy way to do that in regex? As far as I know, the replace method in regex replaces the strings all the way through.

like image 401
Clement Attlee Avatar asked Nov 13 '15 04:11

Clement Attlee


People also ask

How do you replace the last occurrence of a character in a string?

To replace the last occurrence of a character in a string: Use the lastIndexOf() method to get the last index of the character. Call the substring() method twice, to get the parts of the string before and after the character to be replaced. Add the replacement character between the two calls to the substring method.

How do you find the last occurrence of a word in a string?

Return ValueThe strrchr() function returns a pointer to the last occurrence of c in string . If the given character is not found, a NULL pointer is returned. This example compares the use of strchr() and strrchr() . It searches the string for the first and last occurrence of p in the string.

How do I change the last occurrence of a string in Linux?

`sed` command is used in Linux for various types of text operations, such as insert, delete, replace, etc. Different types of replacement tasks can be done by using the `sed` command easily. Any replacement task can be done based on the searching text or pattern.

How do I replace a string in text?

The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.


2 Answers

str.replace() method has a count argument:

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

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

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

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

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end 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.  '
like image 120
Remi Crystal Avatar answered Nov 14 '22 23:11

Remi Crystal


If you want a regex solution, you could match all the ands which are followed by another one later in the string.

>>> 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 which makes sure there is a match later in the string without including it in the actual match (so also not in the substitution). It's sort of like a conditional on the match.

like image 38
tripleee Avatar answered Nov 14 '22 23:11

tripleee