gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:
trying to match words from a list of alternates. The problem is that I cannot seem to stop the match at the root word.
ex:
$words = "(directory|file|age|action)"; while (<>){ if ( $_ !~ /$words/ ){ print "Could not find $_ in \$words\n"; } }
This works for the most part. If I enter a word that is not in the $word var the block executes. If I enter in 'directorys' the block executes, but if I enter in 'directoryy' the block does not execute.
I think that this is due to the fact that perl is greedy by default on matches. To fix this I tried changing the match to:
if ( $_ !~ /$words?/ ){
This did not work, as a matter of fact, it made it so that the code block never executed. Any help is appreciated.
TIA, Chad.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex question
by Aristotle (Chancellor) on Sep 24, 2002 at 17:22 UTC | |
by gnu@perl (Pilgrim) on Sep 24, 2002 at 17:31 UTC | |
by Aristotle (Chancellor) on Sep 24, 2002 at 17:46 UTC | |
|
Re: regex question
by chromatic (Archbishop) on Sep 24, 2002 at 18:56 UTC | |
|
Re: regex question
by VSarkiss (Monsignor) on Sep 24, 2002 at 17:48 UTC | |
by gnu@perl (Pilgrim) on Sep 24, 2002 at 17:54 UTC | |
|
Re: regex question
by Jenda (Abbot) on Sep 24, 2002 at 17:28 UTC | |
by zigdon (Deacon) on Sep 24, 2002 at 17:34 UTC | |
by gnu@perl (Pilgrim) on Sep 24, 2002 at 17:57 UTC | |
by zigdon (Deacon) on Sep 24, 2002 at 18:14 UTC | |
by gnu@perl (Pilgrim) on Sep 24, 2002 at 17:37 UTC | |
|
Re: regex question
by BrowserUk (Patriarch) on Sep 24, 2002 at 17:40 UTC |