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. :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How can grep search based on text or pattern based on user input?
by chromatic (Archbishop) on Dec 02, 2000 at 08:21 UTC | |
by gryphon (Abbot) on Dec 04, 2000 at 22:18 UTC | |
by chromatic (Archbishop) on Dec 04, 2000 at 22:49 UTC | |
by chipmunk (Parson) on Dec 04, 2000 at 23:02 UTC | |
|
Re: How can grep search based on text or pattern based on user input?
by dchetlin (Friar) on Dec 04, 2000 at 11:44 UTC | |
|
Re: How can grep search based on text or pattern based on user input?
by Anonymous Monk on Dec 02, 2000 at 05:35 UTC | |
|
Re: How can grep search based on text or pattern based on user input?
by Anonymous Monk on Dec 02, 2000 at 03:39 UTC |