Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Regular Expressions

by nisha (Sexton)
on Nov 08, 2005 at 07:22 UTC ( [id://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
    Hi,

    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

    Example:

    $string=~m/\Q$filepath\E/

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

    Regards

    Franklin

    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.

    Regards,
    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:

    $filepath="c:\\reports";
    However, the double-quotes will convert the '\\' to a '\', since '\' is a meta-character. You might try using single quotes instead:

    $filepath='c:\\reports';
    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
    Hi

    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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-23 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found