Why do you use \w*? First, if all you have is 2-11, 2-12, etc., then a \d is by far better. Second, by having the * you allow this line to be matched: line2-#THIS LINE BELOW! if ( $contents =~ /^line2-(\w*)/mo ) { print $1; # has to be changed as well } }
Also, as to Hofmator's optimization, I disagree. The /o will have NO effect, since the variable is not in the regex, but is bound to it. If it were $contents =~ /$line/ then it would be an optimization, but now it has no effect, and only muddies up the waters, so to say. Also, I would speculate that the /m is useless in this situation. The format is:
line1: /stuff here/
line2-2: /more stuff/
.
.
.
Therefore, I assume by what he gave us, that the information appears only at the beginning of the lines, and would not make sense to spread multiple lines; therefore taking away the /m would be better(not to mention /m doesn't optimize it, but slightly detracts from it).
UPDATE: By the way, to optimize it further, you should add the caret to the regex; /^line1/ if I am correct in that line1 is at the beginning of the line.
In reply to Re: weird regex problem
by dimmesdale
in thread weird regex problem
by toadi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |