Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Regex expression to match...

by dxxd116 (Beadle)
on Jun 17, 2011 at 18:15 UTC ( #910234=note: print w/ replies, xml ) Need Help??


in reply to Re: Regex expression to match...
in thread Regex expression to match...

Using quantifier can help you avoid repeatting the pattern:

if ($string=~m@(\w+/){2,}@) { print "Dup is $1\n"; }


Comment on Re^2: Regex expression to match...
Download Code
Re^3: Regex expression to match...
by wind (Priest) on Jun 17, 2011 at 19:38 UTC
    Quantifiers used in that way do not do what you think they will:
    use strict; use warnings; my @strings = qw( /help/one/one/one/bar/something_here /buy/cash/buy/water/water/water/baz/nothing_here ); for (@strings) { if (m{(\w+/){2,}}) { print "Dup is '$1'\n"; } } =prints bar/ baz/ =cut

    Even if it did work, you'd still need to add boundary conditions so that a sub directory that is a suffix of the previous directory wouldn't match. Also, if the duplicate is on the end, it wouldn't have a trailing /.

    Don't worry, at one point, I also thought a quantifier should work in that way, but that's specifically why they allow \1 in the LHS.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-09-22 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (189 votes), past polls