newbie question: parse a variable inside an RE?

Discussion in 'Python' started by joemacbusiness@gmail.com, Dec 1, 2008.

  1. Guest

    Hi All,

    How do I parse a variable inside an RE?
    What is the re.search() syntax when your
    search string is a variable?
    It's easy to parse hardcoded RE's but not
    if you use a variable.

    Here is my code, input and runtime:

    $ cat test45.py
    #!/usr/bin/python

    import re

    resp = raw_input('Selection: ')
    newresp = resp.strip()
    print "you chose ", newresp

    fname = open('test44.in')
    for I in fname:
    # if re.search('^newresp', "%s"%(I)): # returns nothing
    # if re.search(^newresp, "%s"%(I)): # syntax error
    if re.search("^newresp", "%s"%(I)): # returns nothing
    print I,

    [jmccaughan@dhcppc2 work]$ cat test44.in
    a1
    b1
    g1
    g2
    h1
    h4
    4g
    5g
    h5

    $ python test45.py
    Selection: g
    you chose g
    $

    Thanks...
    , Dec 1, 2008
    #1
    1. Advertising

  2. schrieb:
    > Hi All,
    >
    > How do I parse a variable inside an RE?
    > What is the re.search() syntax when your
    > search string is a variable?
    > It's easy to parse hardcoded RE's but not
    > if you use a variable.


    Both are exactly equal in difficulty.
    >
    > Here is my code, input and runtime:
    >
    > $ cat test45.py
    > #!/usr/bin/python
    >
    > import re
    >
    > resp = raw_input('Selection: ')
    > newresp = resp.strip()
    > print "you chose ", newresp
    >
    > fname = open('test44.in')
    > for I in fname:
    > # if re.search('^newresp', "%s"%(I)): # returns nothing
    > # if re.search(^newresp, "%s"%(I)): # syntax error
    > if re.search("^newresp", "%s"%(I)): # returns nothing
    > print I,


    How should python know that you want the newresp being expanded? And not
    that you want to search for the word "newresp"?


    You need to use the *variable* newresp:

    if re.search(newresp, I): ...

    If you want to alter it to have a "^" prepended before you use it, you
    need to do so:

    newresp = "^" + newresp

    And as show above,

    "%s" % I

    is nothing but I - no need for the string-interpolation.

    Diez
    Diez B. Roggisch, Dec 1, 2008
    #2
    1. Advertising

  3. John Machin Guest

    Re: newbie question: parse a variable inside an RE?

    On Dec 2, 8:56 am, "Diez B. Roggisch" <> wrote:
    > schrieb:
    >
    > > Hi All,

    >
    > > How do I parse a variable inside an RE?
    > > What is the re.search() syntax when your
    > > search string is a variable?
    > > It's easy to parse hardcoded RE's but not
    > > if you use a variable.

    >
    > Both are exactly equal in difficulty.
    >
    >
    >
    >
    >
    > > Here is my code, input and runtime:

    >
    > > $ cat test45.py
    > > #!/usr/bin/python

    >
    > > import re

    >
    > > resp = raw_input('Selection: ')
    > > newresp = resp.strip()
    > > print "you chose ", newresp

    >
    > > fname = open('test44.in')
    > > for I in fname:
    > > #    if re.search('^newresp', "%s"%(I)):     # returns nothing
    > > #    if re.search(^newresp, "%s"%(I)):       # syntax error
    > >     if re.search("^newresp", "%s"%(I)):      # returns nothing
    > >         print I,

    >
    > How should python know that you want the newresp being expanded? And not
    > that you want to search for the word "newresp"?
    >
    > You need to use the *variable* newresp:
    >
    > if re.search(newresp, I): ...
    >
    > If you want to alter it to have a "^" prepended before you use it, you
    > need to do so:
    >
    > newresp = "^" + newresp
    >
    > And as show above,
    >
    > "%s" % I
    >
    > is nothing but I - no need for the string-interpolation.


    In fact what the OP is trying to do amounts to a convoluted version of
    I.startswith(newresp) which probably isn't his real requirement
    anyway :-(
    John Machin, Dec 1, 2008
    #3
    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. Dave
    Replies:
    0
    Views:
    794
  2. Replies:
    1
    Views:
    432
    Darrin J Olson
    Oct 25, 2003
  3. Replies:
    19
    Views:
    1,119
    Daniel Vallstrom
    Mar 15, 2005
  4. Dave
    Replies:
    0
    Views:
    427
  5. Mehmet Yavuz S. Soyturk

    variable declaration inside eval inside with

    Mehmet Yavuz S. Soyturk, Apr 11, 2007, in forum: Javascript
    Replies:
    3
    Views:
    123
    Mehmet Yavuz S. Soyturk
    Apr 12, 2007
Loading...

Share This Page