N00b question: matching stuff with variables.

K

Ken D'Ambrosio

Hi, all. I've got a file which, in turn, contains a couple thousand
filenames. I'm writing a web front-end, and I want to return all the
filenames that match a user-input value. In Perl, this would be something
like,

if (/$value/){print "$_ matches\n";}

But trying to put a variable into regex in Python is challenging me --
and, indeed, I've seen a bit of scorn cast upon those who would do so in
my Google searches ("You come from Perl, don't you?").

Here's what I've got (in ugly, prototype-type code):

file=open('/tmp/event_logs_listing.txt' 'r') # List of filenames
seek = form["serial"].value # Value from web form
for line in file:
match = re.search((seek)",(.*),(.*)", line) # Stuck here

Clearly, the line, above, is just plain ol' wrong, but I'm including it to
give a hint of what I'm trying to do. What's the correct Python-esque way
to go about this?

Thanks!

-Ken
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top