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>
In reply to Re: weird regex problem
by davorg
in thread weird regex problem
by toadi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |