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.
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.
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.
`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.
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.
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. '
If you want a regex solution, you could match all the and
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With