N00b question: matching stuff with variables.

Rami Chowdhury rami.chowdhury at gmail.com
Mon Jun 28 13:55:49 EDT 2010


On Monday 28 June 2010 10:29:35 Ken D'Ambrosio wrote:
> 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";}

I presume you're suitably sanitizing $value before you arbitrarily use it ;-)?

> 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

Not sure what you're trying to do, here, with the extra regex groups? If you 
just want to naively check for the presence of that user-input value, you can 
do:

seek = sanitize(form["serial"].value)
for line in file:
	match = re.search(seek, line) # re.search(pattern, string, flags)
	if match is not None:
		print "%s matches" % line

One thing that certainly confused me when I learned Python (coming from, among 
other things, Perl) was the lack of implicit variables -- is that what's 
tripping you up?

Hope that helps,
Rami
----
Rami Chowdhury
"Given enough eyeballs, all bugs are shallow." -- Linus' Law
+1-408-597-7068 / +44-7875-841-046 / +88-01819-245544



More information about the Python-list mailing list