Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Regular Expressions

by nisha (Sexton)
on Nov 08, 2005 at 07:22 UTC ( #506636=perlquestion: print w/replies, xml ) Need Help??

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

Hello PerlMonks, I need help with this small problem. I have a variable called
$filepath="c:\\reports"; i also have $string="c:\\reports"; i want to do a pattern match here like if ( $string=~m/$filepath/) { print "Found"; } But here within / / is it possible to give a variable name?? How do we + specify a variable name inside / / in pattern matching.
Please help

Replies are listed 'Best First'.
Re: Regular Expressions
by l.frankline (Hermit) on Nov 08, 2005 at 08:40 UTC

    You can use variable in the pattern matching if you are using regular expressions, but it has a problem, if the variable contains text of special characters, then it is better to use \Q \E



    Warning: \Q \E should be used when you are not using regular expressions in a variable.



    Edit: g0n - corrected code tags

Re: Regular Expressions
by murugu (Curate) on Nov 08, 2005 at 07:50 UTC
    Hi nisha,

    Yes you can give a variable name inside the regular expression.

    Murugesan Kandasamy
    use perl for(;;);

Re: Regular Expressions
by blazar (Canon) on Nov 08, 2005 at 10:50 UTC
    Did you try? What did you get? Short answer: yes, you can put a variable in there. It's interpolated just like in doubly quoted strings. But then you may and should be interested in \Q (and \E). What is this stuff I'm babbling about, anyway? Well you can find out in the same place where you would have also found an answer to your own questions, i.e. precisely in the docs!
Re: Regular Expressions
by tbone1 (Monsignor) on Nov 08, 2005 at 12:43 UTC
    One minor issue. You have:

    However, the double-quotes will convert the '\\' to a '\', since '\' is a meta-character. You might try using single quotes instead:

    Hope this helps.

    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

      However, the double-quotes will convert the '\\' to a '\', since '\' is a meta-character. You might try using single quotes instead:
      1. I think that's exactly what that he wants. But then he should just use forward slashes, since they work just the same even under Windows,
      2. '\' is a meta charachter in single quoted strings too:
      3. $ cat foo print '\\'; $ perl -l foo \
Re: Regular Expressions
by jesuashok (Curate) on Nov 08, 2005 at 07:31 UTC

    you can do that.

    "Keep pouring your ideas"
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2023-02-07 11:07 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (39 votes). Check out past polls.