Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

help in understanding a regex

by Anonymous Monk
on Nov 16, 2012 at 22:14 UTC ( [id://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.

    ...roboticus

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

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found