http://qs1969.pair.com?node_id=545520


in reply to Re: delayed variable interpolation in a regular expression
in thread delayed variable interpolation in a regular expression

I've always thought that the first position was for the closing square bracket, and the hyphen should be placed at the end of the character class. From perlretut: If ’-’ is the first or last character in a character class, it is treated as an ordinary character; "[-ab]","[ab-]" and "[a\-b]" are all equivalent. It's interesting to note that the same doesn't apply to the closing square bracket:

$ perl -e 'my $s="-]"; print "a\n" if $s =~ /[___]]/' $ perl -e 'my $s="-]"; print "a\n" if $s =~ /[]___]/' a $ perl -e 'my $s="-]"; print "a\n" if $s =~ /[___-]/' a $ perl -e 'my $s="-]"; print "a\n" if $s =~ /[-___]/' a

Update: In Perl, ']' has to be escaped so the first and seconds examples above are wrong. It is in grep, where it suffices to put the bracket at the beginning of the character class.

--
David Serrano