in reply to Regex bug or head made of solid bone?

I've tried to play with this code:
$orig='aaa([\+\-]{1})bbb'; $res = '([\\+\\-]{'; $qres = '\\Q$res\\E'; $str=$orig; $str =~ s!\Q$res\E!!; print "Replace against variable quoted => $res <=: $str\n"; $str=$orig; $str =~ s!$qres!!; print "Replace against variable quoted => $qres <=: $str\n";
Examining its output:
Replace against variable quoted => ([\+\-]{ <=: aaa1})bbb Replace against variable quoted => \Q$res\E <=: aaa([\+\-]{1})bbb
I notice that placing special metacharacters inside your variable doesn't make them 'effective'. However, when they are outside of the variable, they get 'noticed' by the regular expression parser. So, could it be the answer to your inquiry? ;-)

_____________________
$"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}

Replies are listed 'Best First'.
Re: Re: Regex bug or head made of solid bone?
by alien_life_form (Pilgrim) on Jun 06, 2002 at 17:06 UTC
    Greetings,

    interesting...
    I wonder whether this is by design or by accident?

    Looking back at my original example, another thing is appears to be non-obvious. The (backslashed) portion that reads:

    \\\\\+\\\- # matches literal \+\- in a variable
    had originally been written as:
    \\\+\\\- # matches literal \+\- in a literal regex
    why do I need an additional '\' before a quoted + and only within a variable is beyond me.... Same reason?
    Cheers,
    alf
    You can't have everything: where would you put it?