in reply to Regular expression help

$& - Contains the string matched by the last pattern match.
use strict; my $string = "Required Not_Required Not_Required Required"; my @words = split(" ",$string); map {$_ =~ /^Required$/g;print "$&"}@words;

Replies are listed 'Best First'.
Re^2: Regular expression help
by JavaFan (Canon) on Oct 23, 2008 at 11:12 UTC
    Some questions regarding your solution.
    1. Why the /g modifier considering that you're matching (in scalar context) against $_ only once before $_ changes?
    2. Why a regexp anyway? Considering that "Required" is a fixed string, and you anchor it on both ends, $_ eq "Required" will do as well.
    3. Why quotes around $&?