in reply to weird regex problem
Well there are quite a few typos in your code which mean that it doesn't compile, but I assume they are just transcription errors.
Fixing them and running your code, I see that nothing is output as you describe. The problem is in the line
if ( $contents =~ /(line2-)(\w*)/g ) {The /g is unnecessary here and is causing the expression to evaluate as false. Removing the /g makes the code work as expected.
The /g option matches the regex as often as it can against your string. The final time it tries to match, the match fails and the operator returns false. Without the /g the match only takes place the one time it needs to succeed and the operator returns true.
Update: Yeah. As others in this thread point out, I had the right fix, but the wrong explanation. Should drink more coffee before posting :)
--Perl Training in the UK <http://www.iterative-software.com>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: weird regex problem
by Hofmator (Curate) on Jun 14, 2001 at 15:57 UTC |