Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

backslash in regex

by arc_of_descent (Hermit)
on Feb 02, 2003 at 10:30 UTC ( [id://231976]=perlquestion: print w/replies, xml ) Need Help??

arc_of_descent has asked for the wisdom of the Perl Monks concerning the following question:


Hi,

What am i doing wrong?

$fav = 'd:\stuff'; $dir = 'd:\stuff\buff\tuff'; if ($dir =~ /^\Q$fav\\/) { print "Match!\n"; } else { print "No match!\n"; }

This prints "No match!"
Thanx
--
arc_of_descent

Replies are listed 'Best First'.
Re: backslash in regex
by JaWi (Hermit) on Feb 02, 2003 at 10:49 UTC
    The \Q disables pattern metacharacters until a \E is found, thus, if you wrote your regexp like:
    if ( $dir =~ /^\Q$fav\E\\/ ) { # Note the \E after $fav!
    It will match the stuff you want...

    Update: placed the \E at the wrong place...

    -- JaWi

    "A chicken is an egg's way of producing more eggs."

       if ( $dir =~ /^\Q$fav\\\E/ ) { # Note the \E at the end! won't work -- the problem is under \Q modifier the backslash at the end matches wrong  if ( $dir =~ /^\Q$fav\E\\/ ) { works
      --
      Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
      >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 22:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found