Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

help in understanding a regex

by Anonymous Monk
on Nov 16, 2012 at 22:14 UTC ( #1004251=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/local/bin/perl -w $string = "/dir1/dir8/dir2"; $string =~ m|([^/]+)$|; print $1;

if i change the line of code as $string =~ m/([^/]+)$/ it doesent work.

Comment on help in understanding a regex
Select or Download Code
Re: help in understanding a regex
by roboticus (Chancellor) on Nov 16, 2012 at 22:36 UTC

    Yeah, you'd have to change it to m/([^\/]+)$/ to work. The original used vertical bars so they didn't have to escape the slash in the brackets.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: help in understanding a regex
by 2teez (Priest) on Nov 16, 2012 at 22:56 UTC

    with $string =~ m/[^/]+)$/; using strict and warnings, you would get unmatched error [ in regex ...
    Please note the usage of the "/" as the delimiter,
    which you have to "escape" to use within the m// like so: $string =~ m/[^\/]+)$/;
    Please check the subtitle m/PATTERN/msixpodualgc under Regexp-Quote-Like-Operators

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2015-07-03 23:22 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 (57 votes), past polls