Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

regexp matching with "\[" in the substring to be matched

by ISAI student (Scribe)
on Jul 05, 2005 at 11:48 UTC ( #472421=perlquestion: print w/ replies, xml ) Need Help??
ISAI student has asked for the wisdom of the Perl Monks concerning the following question:


hello, below is a code which logic is unexplicable to me:
#!/bin/perl -w $p= "horraen=sumsel\[13\]\)\n"; $m="sumsel\[13\]"; print join("","this is \$m:",$m,"\n"); print $p; if ($p=~m/en=$m\)/) {print "yeah!\n";} else {print "why isn't it matching?\n";} $m=~s/\[/\\\[/g; $m=~s/\]/\\\]/g; print join("","this is \$m:",$m,"\n"); if ($p=~m/en=$m\)/) {print "yeah!\n";} else {print "what will I do now?\n";}
Why do I need the add an addition "\\" before the sqare braces (in the substring) when matching the substring? -ISAI student

Comment on regexp matching with "\[" in the substring to be matched
Download Code
Re: regexp matching with "\[" in the substring to be matched
by rev_1318 (Chaplain) on Jul 05, 2005 at 11:54 UTC
    Because your var ($p) contains a backslash follows by a square bracket (\[). $m gets interpolated in the RE, so in the first case, the \[ will be interpolated as only a square bracket, and you lose the backslash.

    Paul

Re: regexp matching with "\[" in the substring to be matched
by prasadbabu (Prior) on Jul 05, 2005 at 12:13 UTC

    As rev_1318 said, you should always escape the special characters by giving backslash.

    Also you go through perlre.

    Prasad

Re: regexp matching with "\[" in the substring to be matched
by nobull (Friar) on Jul 05, 2005 at 16:39 UTC
    $p="horraen=sumsel\[13\]\)\n"; $m="sumsel\[13\]";

    The above are exactly equivalent to

    $p="horraen=sumsel[13])\n"; $m="sumsel[13]";

    If you really want backslashes in the strings you need to double them or use single quotes.

    $p="horraen=sumsel\\[13\\]\\)\n"; $m="sumsel\\[13\\]";
    ..or..
    $p='horraen=sumsel\[13\]\)' . "\n"; $m='sumsel\[13\]';

    Actually since $m is a regex it's more ideomatic to use qr// unless there's a reason you can't. Also I don't think you really wanted backslashes in $p.

    $p="horraen=sumsel[13])\n"; $m=qr/sumsel\[13\]/;
Re: regexp matching with "\[" in the substring to be matched
by tlm (Prior) on Jul 06, 2005 at 02:12 UTC

    Also, you can use \Q...\E to quote otherwise regex-meaningful characters. E.g.

    $p = "horraen=sumsel[13])\n"; # no backslashes required $m = qr/\Qsumsel[13]\E/; # actually in this case qr/\Qsumsel[13]/ is +enough print $p; if ( $p =~ /$m\)/ ) { print "yeah!\n"; } else { print "why isn't it matching?\n"; } __END__ horraen=sumsel[13]) yeah!
    See also quotemeta.

    the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://472421]
Approved by rev_1318
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2015-07-02 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (36 votes), past polls