re.sub question (regular expressions)

C

Chris Seberino

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
 
J

Jean-Michel Pichavant

Chris said:
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 :eek:)

JM
 
M

MRAB

Chris said:
What does this line do?...

input_ = re.sub("([a-zA-Z]+)", '"\\1"', input_)
Why don't you try it?
Does it remove parentheses from words?
e.g. (foo) -> foo ???
No, it puts quotes around them.
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?
What do you mean "blow up"? It worked for me in Python v2.6.2.
 
C

Chris Seberino

What do you mean "blow up"? It worked for me in Python v2.6.2.

My bad. False alarm. This was one of those cases where a bug in
another area appears like a bug in a different area.

Thank for the help.

cs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top