Python student seeks help regex/strings

John Machin sjmachin at
Sun Apr 12 08:10:33 CEST 2009

On Apr 12, 2:30 pm, Petyr David <phyn... at> wrote:
> I'm familiar with simple string substitutions in shell and perl:
> string= "I have 30 days to find it"
> echo $string | sed "s/\(.*\)\(30\)\(.*\)/\2/
> which will (if I did it right) leave 30
> and perl likewise.
> I'm having trouble figuring out how to do this in Python and it's not
> for lack of trying to find an example. It can't be this hard is it?

No, it isn't; there are examples in the docs (
library/re.html#re.sub) and the HOWTO (
howto/regex.html#search-and-replace) ... how did you manage to avoid
finding them?

 >>> import re
 >>> strg = "yadda 30 blah"
 >>> re.sub(r"(.*)(30)(.*)", r"\2", strg)

More information about the Python-list mailing list