Replace in Python- * equivalent?

If I find and replace some text, how can I get it to replace some text that will change every day, i.e. anything between ((and)), whatever it is?

Hooray!

0


a source to share


1 answer


Use regular expressions ( http://docs.python.org/library/re.html )?

Could you be more specific, I don't think I fully understand what you are trying to achieve.

EDIT:

Ok, now I see. This could be done even easier, but here goes:



>>> import re

>>> s = "foo(bar)whatever"
>>> r = re.compile(r"(\()(.+?)(\))")
>>> r.sub(r"\1baz\3",s)
'foo(baz)whatever'

      

For multiple levels of parentheses, this won't work, or rather will work, but will do what you probably don't want it to do.

Oh hey, as a bonus, it's the same regex, only now it replaces the string in the nearest parentheses:

r1 = re.compile(r"(\()([^)^(]+?)(\))")

      

+4


a source







All Articles