re.sub and variables

Discussion in 'Python' started by fuglyducky, Aug 12, 2010.

  1. fuglyducky

    fuglyducky Guest

    I have a function that I am attempting to call from another file. I am
    attempting to replace a string using re.sub with another string. The
    problem is that the second string is a variable. When I get the
    output, it shows the variable name rather than the value. Is there any
    way to pass a variable into a regex?

    If not, is there any other way to do this? I need to be able to dump
    the variable value into the replacement string.

    For what it's worth this is an XML file so I'm not afraid to use some
    sort of XML library but they look fairly complicated for a newbie like
    me.

    Also, this is py3.1.2 is that makes any difference.

    Thanks!!!

    #####################################################

    import random
    import re
    import datetime

    def pop_time(some_string, start_time):
    global that_string

    rand_time = random.randint(0, 30)
    delta_time = datetime.timedelta(seconds=rand_time)

    for line in some_string:
    end_time = delta_time + start_time
    new_string = re.sub("thisstring", "thisstring\\end_time",
    some_string)
    start_time = end_time

    return new_string
     
    fuglyducky, Aug 12, 2010
    #1
    1. Advertising

  2. fuglyducky

    fuglyducky Guest

    On Aug 12, 2:06 pm, fuglyducky <> wrote:
    > I have a function that I am attempting to call from another file. I am
    > attempting to replace a string using re.sub with another string. The
    > problem is that the second string is a variable. When I get the
    > output, it shows the variable name rather than the value. Is there any
    > way to pass a variable into a regex?
    >
    > If not, is there any other way to do this? I need to be able to dump
    > the variable value into the replacement string.
    >
    > For what it's worth this is an XML file so I'm not afraid to use some
    > sort of XML library but they look fairly complicated for a newbie like
    > me.
    >
    > Also, this is py3.1.2 is that makes any difference.
    >
    > Thanks!!!
    >
    > #####################################################
    >
    > import random
    > import re
    > import datetime
    >
    > def pop_time(some_string, start_time):
    >     global that_string
    >
    >     rand_time = random.randint(0, 30)
    >     delta_time = datetime.timedelta(seconds=rand_time)
    >
    >     for line in some_string:
    >         end_time = delta_time + start_time
    >         new_string = re.sub("thisstring", "thisstring\\end_time",
    > some_string)
    >         start_time = end_time
    >
    >     return new_string


    Disregard...I finally figured out how to use string.replace. That
    appears to work perfectly. Still...if anyone happens to know about
    passing a variable into a regex that would be great.
     
    fuglyducky, Aug 12, 2010
    #2
    1. Advertising

  3. fuglyducky

    John Machin Guest

    On Aug 13, 7:33 am, fuglyducky <> wrote:
    > On Aug 12, 2:06 pm, fuglyducky <> wrote:
    >
    >
    >
    > > I have a function that I am attempting to call from another file. I am
    > > attempting to replace a string using re.sub with another string. The
    > > problem is that the second string is a variable. When I get the
    > > output, it shows the variable name rather than the value. Is there any
    > > way to pass a variable into a regex?

    >
    > > If not, is there any other way to do this? I need to be able to dump
    > > the variable value into the replacement string.

    >
    > > For what it's worth this is an XML file so I'm not afraid to use some
    > > sort of XML library but they look fairly complicated for a newbie like
    > > me.

    >
    > > Also, this is py3.1.2 is that makes any difference.

    >
    > > Thanks!!!

    >
    > > #####################################################

    >
    > > import random
    > > import re
    > > import datetime

    >
    > > def pop_time(some_string, start_time):
    > >     global that_string

    >
    > >     rand_time = random.randint(0, 30)
    > >     delta_time = datetime.timedelta(seconds=rand_time)

    >
    > >     for line in some_string:
    > >         end_time = delta_time + start_time
    > >         new_string = re.sub("thisstring", "thisstring\\end_time",
    > > some_string)
    > >         start_time = end_time

    >
    > >     return new_string

    >
    > Disregard...I finally figured out how to use string.replace. That
    > appears to work perfectly. Still...if anyone happens to know about
    > passing a variable into a regex that would be great.


    Instead of

    new_string = re.sub(
    "thisstring", "thisstring\\end_time", some_string)

    you probably meant to use something like

    new_string = re.sub(
    "thisstring", "thisstring" + "\\" + end_time, some_string)

    string.replace is antique and deprecated. You should be using methods
    of str objects, not functions in the string module.

    >>> s1 = "foobarzot"
    >>> s2 = s1.replace("bar", "-")
    >>> s2

    'foo-zot'
    >>>
     
    John Machin, Aug 13, 2010
    #3
  4. On Thu, 12 Aug 2010 14:33:28 -0700, fuglyducky wrote:

    > if anyone happens to know about
    > passing a variable into a regex that would be great.


    The same way you pass anything into any string.

    Regexes are ordinary strings. If you want to construct a string from a
    variable t = "orl", you can do any of these:

    "Hello w" + t + "d"

    "".join(["Hello", " ", "w", t, "d"])

    "Hello w%sd" % t

    "Hello %s" % ("w" + t + "d")

    "Hello w%(var)sd" % {"var": t}

    "Hello wSPAMd".replace("SPAM", t)

    or many other variations. Constructing a regex is no different.


    --
    Steven
     
    Steven D'Aprano, Aug 13, 2010
    #4
  5. fuglyducky

    MRAB Guest

    Steven D'Aprano wrote:
    > On Thu, 12 Aug 2010 14:33:28 -0700, fuglyducky wrote:
    >
    >> if anyone happens to know about
    >> passing a variable into a regex that would be great.

    >
    > The same way you pass anything into any string.
    >
    > Regexes are ordinary strings. If you want to construct a string from a
    > variable t = "orl", you can do any of these:
    >
    > "Hello w" + t + "d"
    >
    > "".join(["Hello", " ", "w", t, "d"])
    >
    > "Hello w%sd" % t
    >
    > "Hello %s" % ("w" + t + "d")
    >
    > "Hello w%(var)sd" % {"var": t}
    >
    > "Hello wSPAMd".replace("SPAM", t)
    >
    > or many other variations. Constructing a regex is no different.
    >

    You just need to remember that if you pass a string into re.sub as a
    replacement then it'll be treated as a template. It's all in the
    documentation! :)
     
    MRAB, Aug 13, 2010
    #5
    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. THY
    Replies:
    1
    Views:
    398
    Steve C. Orr, MCSD
    Aug 19, 2003
  2. Kathy Burke
    Replies:
    3
    Views:
    399
    Kathy Burke
    Nov 24, 2003
  3. Tee
    Replies:
    2
    Views:
    817
  4. Ben
    Replies:
    2
    Views:
    905
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,056
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page