newbie question: parse a variable inside an RE?

joemacbusiness at gmail.com joemacbusiness at gmail.com
Mon Dec 1 15:47:23 EST 2008


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 at dhcppc2 work]$ cat test44.in
a1
b1
g1
g2
h1
h4
4g
5g
h5

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

Thanks...



More information about the Python-list mailing list