Sebastian
Sebastian

Reputation: 75

Use placeholder inside .replace()

With Python I need to replace a text but parts of the text is dynamical created. The start and finish of the string is always the same and unique.

Can I use placeholders inside filedata.replace() or is there a different method to solve that problem?

This did not work:

filedata = filedata.replace('<a href="/link/%s">Link</a>', '<a href="/link/replaced">Link</a>')

Upvotes: 0

Views: 647

Answers (1)

Sebastian
Sebastian

Reputation: 75

Solution based on the comments under my question:

import re
filedata = re.sub('<a href="/link/(.*?)">Link</a>', '<a href="/link/replaced">Link</a>', filedata)

Read about regular expressions.

Upvotes: 1

Related Questions