Home > database >  Regex add character before word
Regex add character before word

Time:12-09

import re
text = 'https://app.propertymeld.com/1568/v/23256/meld/4376491/'
d = re.sub(r'(meld)', r'\1s', text)
e = re.sub(r'api',r'(/meld)', d)
print(e)

required solution : "https://app.propertymeld.com/1568/v/23256/api/melds/4376491/tenant-files/"

Is there any solution to add the word api before the word melds.

CodePudding user response:

Try:

import re

text = 'https://app.propertymeld.com/1568/v/23256/meld/4376491/'

e = re.sub(r'/meld', '/api/melds', text)

print(e)

OUTPUT:

https://app.propertymeld.com/1568/v/23256/api/melds/4376491/


Note:

If you want tenant-files/ at the end, then

e = re.sub(r'/meld', '/api/melds', text)   "tenant-files/"

output:

https://app.propertymeld.com/1568/v/23256/api/melds/4376491/tenant-files/

CodePudding user response:

Another way is here.

import re
text = 'https://app.propertymeld.com/1568/v/23256/meld/4376491/'
e = re.sub(r'(/meld)', r'/api\1s', text)
print(e)

output

https://app.propertymeld.com/1568/v/23256/api/melds/4376491/
  • Related