Sandun Dayananda
Sandun Dayananda

Reputation: 31

Replace character and previous character in Python

I need to replace each "්" and the previous character to it from the following paragraph. How can do that with Python 3?

paalau jaiwaiThe thanaiya makana්nata dawasaka්‌ oba mata hamauwauwaa andaurau maawaThe atha්waela්‌ baendagarena apa daredarenaa paiyamna්‌ karerauwaa kaisaidaa nomaekarena mathake raendailaa oba maa haeradaa warena්wauwaa obatama urauma magema saihainaya obage ahase laiyawewaa

Upvotes: 0

Views: 46

Answers (1)

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

You're looking for re.sub():

import re
result = re.sub(".?්", "[REPLACE WITH]", "paalau jaiwaiThe thanaiya makana්nata dawasaka්‌ oba mata hamauwauwaa andaurau maawaThe atha්waela්‌ baendagarena apa daredarenaa paiyamna්‌ karerauwaa kaisaidaa nomaekarena mathake raendailaa oba maa haeradaa warena්wauwaa obatama urauma magema saihainaya obage ahase laiyawewaa")
print(result)

Explanation

  • The first argument is a regex that matches , any single character that precedes it (.), if there is one (?).
  • The second argument is the text you want to replace these characters with.
  • The third and last argument is your source string.

Upvotes: 3

Related Questions