Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Regex help

by yoda54 (Monk)
on Sep 10, 2012 at 03:40 UTC ( #992654=perlquestion: print w/ replies, xml ) Need Help??
yoda54 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I don't understand why my $3 includes the slash and everything after and not just "to"?

Thanks for any advice!

#!/usr/bin/perl use strict; use warnings; $a = "some/path/to/somewhere/hello"; if ($a =~ /(.+)(\/path\/)(.+)(\/.+)/) { print "$1 -> $3\n"; } Output: some -> to/somewhere

Comment on Regex help
Download Code
Re: Regex help
by Anonymous Monk on Sep 10, 2012 at 03:58 UTC
    Because it does :) Add
    use re 'debug';
    and watch the regex engine match the thing

    Also see

    use YAPE::Regex::Explain; print YAPE::Regex::Explain->new( qr/(.+)(\/path\/)(.+)(\/.+)/ )->explain; __END__ The regular expression: (?-imsx:(.+)(/path/)(.+)(/.+)) 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): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .+ any character except \n (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- /path/ '/path/' ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- ( group and capture to \3: ---------------------------------------------------------------------- .+ any character except \n (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \3 ---------------------------------------------------------------------- ( group and capture to \4: ---------------------------------------------------------------------- / '/' ---------------------------------------------------------------------- .+ any character except \n (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \4 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
      Thanks!
Re: Regex help
by GrandFather (Cardinal) on Sep 10, 2012 at 04:00 UTC

    + is greedy and will match as many of whatever it applies to as it can. Generally it is smart to avoid . and use an explicit set of characters to match. In this case everything except / looks like it may be what you want:

    $str =~ m!(.+)(/path/)([^/]+)(/.+)!

    Note that I used ! as the regex delimiter to avoid having to quote all the / characters.

    True laziness is hard work
      Thank you sir! :-D

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992654]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-09-03 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls