Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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
Replies are listed 'Best First'.
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.


    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?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2015-10-10 14:48 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (257 votes), past polls