re.sub question (regular expressions)

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Oct 16 12:50:32 EDT 2009


Chris Seberino wrote:
> What does this line do?...
>
> input_ = re.sub("([a-zA-Z]+)", '"\\1"', input_)
>
> Does it remove parentheses from words?
> e.g. (foo) -> foo ???
>
> I'd like to replace [a-zA-Z] with \w but \w makes it blow up.
>
> In other words, re.sub("(\w+)", '"\\1"', input_) blows up.
>
> Why?
>
> cs
>   
( has a special meaning of not preceded by \ (see re help)

the first line transform 'foo' into '"foo"'
your solution is doing the same.

If I'm wrong, MRAB will come into rescue anyway :o)

JM



More information about the Python-list mailing list