re module substitution confusion

Discussion in 'Python' started by Doug Farrell, Jul 7, 2003.

  1. Doug Farrell

    Doug Farrell Guest

    Hi all,

    I'm trying to do the following from within a code module:

    import re

    # text to match
    text = "Good morning x something /x, how are you today x something
    else /x"

    # pattern to match
    regex = re.compile("(x)(.*?)(/x)", re.DOTALL)

    # find first match
    m = re.search(regex, text)

    # keep looking while there are matches
    while m != None:
    # substitute in some other text for the exact match
    text = re.sub(m.group(), "Mr. Phelps", text)

    # find the next match
    m = re.search(regex, text)

    print text

    This works within the Python shell, but I can't seem to make it work
    from within a program. It would seem like the re.sub(m.group()...)
    would work fine as m.group() just returns the matched string
    completely.

    Any help would be greatly appreciated,
    Doug Farrell
     
    Doug Farrell, Jul 7, 2003
    #1
    1. Advertising

  2. Doug Farrell

    Doug Farrell Guest

    Hi all,

    I got a direct email from someone (who I can't remember right now) who
    suggested that I wasn't very clear about what I was trying to do and
    what was wrong. In an attempt to clarify I'm adding this post. :)

    I'm trying to create a little Python CGI program that will read in a
    HTML template file that has Python code embedded in it between
    <python> ... </python> tags. So I'd like to use the re module to find
    the following pattern:

    pattern = re.compile("(<python>)(.*?)(</python>)", re.DOTALL)

    Doing a search with this pattern like this:

    match = re.search(pattern, text)

    where text equals the HTML template file in string form, will find all
    the code segments. I then pull out the code segment and pass it to
    exec() to execute it. I can capture the output of the exec() call by
    redirecting sys.stdout to a StringIO object. The problem is that this;
    I can match the <python>...</python> segments no problem. But I only
    want to replace each one in turn with the results of the exec() call.
    If I do this:

    text = re.sub(pattern, ".. exec() output..", text) it replaces all the
    code segments, not just the one that was matched. I need to substitute
    in the output of the exec() (which is a string when I'm done) into the
    one place it came from.

    Hope this clears up any confusion.

    I didn't include the whole program in my first post as it's kind of
    long. I thought the sections I included above were enough information
    to follow what I'm trying to do, at least I hope so.

    Thanks again in advance for your help,
    Doug
     
    Doug Farrell, Jul 8, 2003
    #2
    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. Replies:
    6
    Views:
    327
    Magnus Lycka
    Oct 7, 2005
  2. Maric Michaud
    Replies:
    0
    Views:
    7,222
    Maric Michaud
    Jun 24, 2006
  3. rjcarr

    module confusion

    rjcarr, Oct 2, 2007, in forum: Python
    Replies:
    46
    Views:
    1,386
    Marc 'BlackJack' Rintsch
    Oct 6, 2007
  4. Jugdish
    Replies:
    3
    Views:
    349
    Scott David Daniels
    Jan 1, 2008
  5. Henry Law
    Replies:
    7
    Views:
    128
    John Bokma
    Nov 20, 2005
Loading...

Share This Page