2
for j in Xjoints:
    j = substitute( XrigNamespace, j, '')

I'm looking to write a Python equivalent for Substitute, any suggestions would be greatly appreciated. Thanks!

Adam Sirrelle
  • 357
  • 8
  • 18

1 Answers1

3

Regular expression substitution:

import re
test = "Hello -%there%-"
regularExpr = "-%.*%-"
s1 = re.sub(regularExpr, "Mel", test)
# Result: Hello Mel
s2 = re.sub("foo", "Mel", test)
# Result: Hello -%there%-

While it's obviously possible to write an equivalent in Python, the high level libraries already provided are there to be used (and will in this case certainly perform better).

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26