in reply to Re: This regular expression has me stumped
in thread This regular expression has me stumped

Hmm your solution looks like its working! Great. Now the big problem. I cannot make a head or tail of the regexp :( could you explain me a little bit on what exactly happened up there. It made a whooshing sound and flew right by :)
  • Comment on Re^2: This regular expression has me stumped

Replies are listed 'Best First'.
Re^3: This regular expression has me stumped
by GrandFather (Saint) on May 01, 2008 at 12:10 UTC

    :-D

    Ok, let's take it a a little at a time:

    s! you know, although it's possible you didn't know you can use pretty much any character for the expression delimiters.

    (?:^|/) matches (without capturing) either the start of the string or a /.

    [^\s,@;]* matches as many characters that aren't in the set of terminal characters as can be found.

    (?<=/) looks back and asserts the last character matched was /.

    ([^\s,@;]+?) matches and captures as few non-terminal characters as it can and still find a match. That's the filename that you want.

    (?=[\s,@;]|$) looks ahead and asserts that the next character is a terminal character or the end of the string.

    !$1!g you are probably completely familiar with - replace all the matched stuff with the captured string and do it for every match that can be found.

    So with a little head scratching the introductory line of my initial reply might make a more sense along with the regex. For further study consult perlretut, perlre and perlreref.


    Perl is environmentally friendly - it saves trees
Re^3: This regular expression has me stumped
by toolic (Bishop) on May 01, 2008 at 13:23 UTC
    To supplement GrandFather's excellent explanation, here is the output generated by YAPE::Regex::Explain.
    use warnings; use strict; use YAPE::Regex::Explain; my $re = 's!(?:^|/)[^\s,@;]*(?<=/)([^\s,@;]+?)(?=[\s,@;]|$)!$1!g'; my $parser = YAPE::Regex::Explain->new($re); print $parser->explain;