Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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.

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 (Vicar) 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 not a whimper to be heard...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-20 06:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.