in reply to Regex explanation

the regex match anything anytime ('.*') then a unix path separator ( '\/' i.e. '/' but escaped, because is the same separator used in this regex), then capture (and put it in $1) followed by anychar one time ( '.' ERROR: the dot must be escaped if you want a real lonely dot!) and endly a 'lib' string. All the matched string will be subsituted by what is in the captured variable ($1).

But YAPE::Regex::Explain is our friend!

perl -MYAPE::Regex::Explain -e "my $rex = qr(s/.*\/(.*).lib/$1/); prin +t YAPE::Regex::Explain->new($rex)->explain" The regular expression: (?-imsx:s/.*\/(.*).lib//) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- s/ 's/' ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \/ '/' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- lib// 'lib//' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

L*
UPDATE: as stated by wise monks below the module does not handle substitutions and in the case above produce somehow misleading output (as lib//). So better to reduce the regex to a non substituiton one and inspect the output to see where the error is (at the dot).
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new('.*\/( +.*).lib')->explain" The regular expression: (?-imsx:.*\/(.*).lib) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \/ '/' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- lib 'lib' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Regex explanation
by choroba (Cardinal) on Sep 14, 2015 at 10:45 UTC
    YAPE::Regex::Explain doesn't handle substitutions, it can only explain regular expressions. Substitution is a regular expression plus pattern to replace the matching part, the pattern usually works as double quoted string (unless /e is involved).
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      yes, you are right choroba and in fact in the explanation given there is no mention of the substitution. Anyway it handles correctly the interesting part and also in this case helps a lot in spotting the wrong part.

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        ... it handles correctly the interesting part ...

        But it handles the incorrect part in a confusing and misleading way.

        ... and ... helps a lot in spotting the wrong part.

        Whether it helps "a lot" or at all is open to question. But why not just fix the wrong part? Please see How do I change/delete my post?. (Of course, I'm suggesting changing your reply, not deleting it!) Perhaps also mention that YAPE::Regex::Explain only supports regex features added through Perl version 5.6.


        Give a man a fish:  <%-{-{-{-<