http://www.perlmonks.org?node_id=44553

Russ has asked for the wisdom of the Perl Monks concerning the following question:

I've got a CGI that accepts a single form field (name=searchtext) which I put into $searchtext. I'd like to offer the un-Perl-knowledgeable user to simply enter a word with * wildcards like any normal search, but at the same time offer the Perl-knowledgeable user the ability to enter a pattern.

I'm assuming that the un-Perl will enter something like "surf*g" and the Perl user will enter something like "/surf\w*/i" or similar. (I'm assuming that all searches by the un-Perl are case insensitive.)

Here's what I have so far:

if ($searchtext =~ /^\/.*\/\w+$/) { $searchtext =~ s#^\s+/|/\s+$|/\w+\s+$#/#g; } else { $searchtext =~ s/([\\\+\$\^])/\\$1/g; $searchtext =~ s/\*/\\w\*/g; $searchtext = '/' . $searchtext . '/i'; } foreach (sort {$a <=> $b} grep $searchtext, grep !/^#/, <STUFF>) {

Problem is, this doesn't work. And even if it did, I'm sure it's nowhere close to being the most efficient code around. Any suggestions? Thanks. :)