re.sub question (regular expressions)

Discussion in 'Python' started by Chris Seberino, Oct 16, 2009.

  1. 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
    Chris Seberino, Oct 16, 2009
    #1
    1. Advertising

  2. 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 :eek:)

    JM
    Jean-Michel Pichavant, Oct 16, 2009
    #2
    1. Advertising

  3. Chris Seberino

    MRAB Guest

    Chris Seberino wrote:
    > 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.
    MRAB, Oct 16, 2009
    #3
  4. On Oct 16, 9:51 am, MRAB <> wrote:

    > 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
    Chris Seberino, Oct 20, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    584
    Jay Douglas
    Aug 15, 2003
  2. Vibha Tripathi
    Replies:
    3
    Views:
    2,181
    George Sakkis
    Jul 5, 2005
  3. Ben
    Replies:
    2
    Views:
    853
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,940
    Lawrence D'Oliveiro
    May 20, 2011
  5. Noman Shapiro
    Replies:
    0
    Views:
    216
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page