Nalina has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks
In one of the scripts I found the following RE line
($thisj) = $dir =~ m#/([^/]+)$#;
If $dir = "/exp/01jehandel/userinput/runwhennewfile"; then $thisj will have runwhennewfile.
Please tell me how is the matching done? Also what difference does putting brackets '()' for $thisj in its assignment statement makes?
what is '#' doing here? It will be helpful if some good links for such RE's is provided
Thanks & Regards
Nalina

Replies are listed 'Best First'.
Re: Perl Regular Exp
by ysth (Canon) on Dec 28, 2006 at 09:27 UTC
    The () around $thisj make the following = be interpreted as a list assignment, so the thing after the = (the match on $dir) is given list context, and $thisj is assigned the first (and only, in this case) element of the list returned by the match. See m/PATTERN/ in http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators-operator%2c-regexp for what a regex match returns in list vs. scalar context.

    m introduces a regex to be used for matching; whatever follows it is the delimeter around the regex. m!/([^/]+)$! would be exactly the same, as would m/\/([^\/]+)$/ (note that with / as the delimeter, /'s in the regex need to be escaped with \).

Re: Perl Regular Exp
by ferreira (Chaplain) on Dec 28, 2006 at 09:37 UTC
    Please tell me how is the matching done?

    The m#/([^/]+)$# matches a string of one or more non-slash characters at the end of a single-line string. (1) One or more is implied by +. (2) non-slash is implied by [^/]. (3) at the end of the string is implied by $. So when you feed the regex with $dir = "/exp/01jehandel/userinput/runwhennewfile", it matches runwhennewfile as you said.

    Also what difference does putting brackets '()' for $thisj in its assignment statement makes?

    The '()' capture the enclosed regex piece into the first group, $1. As a list assignment is being done, in ($thisj) = $dir =~ ..., that means the matching groups are returned to the left side value. But there's only one group here and $thisj will get its contents.

    what is '#' doing here?

    As the regex uses literal slashes (/), another delimiter was chosen (in the case, #). That what means m# ... #. Otherwise, it should have been written as /\/([^\/]+)$/ (which is hard to read).

    You may learn more at perlretut and perlre.

Re: Perl Regular Exp
by virtualsue (Vicar) on Dec 28, 2006 at 10:48 UTC
Re: Perl Regular Exp
by johngg (Canon) on Dec 28, 2006 at 11:46 UTC
    It is probably worth pointing out what would happen if there weren't the parentheses "()" on the LHS of the assignment. Without the parentheses the assignment is in scalar rather than list context and records whether the match was successful (1) or not (empty string '') rather than assigning the list of captures in the match. The following script illustrates this

    use strict; use warnings; my $dir = q{/some/path/to/afile}; print qq{\nMatching $dir\n}; print qq{\nWith parentheses, list context\n}; my ($fileName) = $dir =~ m{([^/]+)$}; print defined $fileName ? qq{-->$fileName<--\n} : qq{Not defined\n}; print qq{\nWithout parentheses, scalar context\n}; $fileName = $dir =~ m{([^/]+)$}; print defined $fileName ? qq{-->$fileName<--\n} : qq{Not defined\n}; $dir = q{/some/path/with/no/file/}; print qq{\nMatching $dir\n}; print qq{\nWith parentheses, list context\n}; ($fileName) = $dir =~ m{([^/]+)$}; print defined $fileName ? qq{-->$fileName<--\n} : qq{Not defined\n}; print qq{\nWithout parentheses, scalar context\n}; $fileName = $dir =~ m{([^/]+)$}; print defined $fileName ? qq{-->$fileName<--\n} : qq{Not defined\n};

    Here's the output

    Matching /some/path/to/afile With parentheses, list context -->afile<-- Without parentheses, scalar context -->1<-- Matching /some/path/with/no/file/ With parentheses, list context Not defined Without parentheses, scalar context --><--

    I hope this is of use.

    Cheers,

    JohnGG

Re: Perl Regular Exp
by ambrus (Abbot) on Dec 28, 2006 at 11:16 UTC
Re: Perl Regular Exp
by Anonymous Monk on Dec 28, 2006 at 12:13 UTC
Re: Perl Regular Exp
by Anonymous Monk on Dec 28, 2006 at 12:14 UTC