tags:

views:

107

answers:

1

If I am finding & replacing some text how can I get it to replace some text that will change each day so ie anything between (( & )) whatever it is?

Cheers!

+4  A: 

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

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

EDIT:

Ok, now I see. This may 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 will not work, or rather it WILL work, but will do something you probably don't want it to do.

Oh hey, as a bonus here's the same regular expression, only now it will replace the string in the innermost parentheses:

r1 = re.compile(r"(\()([^)^(]+?)(\))")
shylent
One of this days I'm going to have to make myself learn about regex... I hate it when others can, with a few characters of nonsense, achieve what would take several lines of code for me...
Jaime