Finding last occurrence of substring in string, replacing that

前端 未结 7 1660
暗喜
暗喜 2021-01-31 01:07

So I have a long list of strings in the same format, and I want to find the last \".\" character in each one, and replace it with \". - \". I\'ve tried using rfind, but I can\'t

7条回答
  •  忘了有多久
    2021-01-31 01:18

    You can use the function below which replaces the first occurrence of the word from right.

    def replace_from_right(text: str, original_text: str, new_text: str) -> str:
        """ Replace first occurrence of original_text by new_text. """
        return text[::-1].replace(original_text[::-1], new_text[::-1], 1)[::-1]
    

提交回复
热议问题