http://www.perlmonks.org?node_id=1004251

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