http://www.perlmonks.org?node_id=1012836

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

Please help in writing the regex. match the url below
https://www.abc.com/ap/ap/top-news/76/nTk3M/ https://www.abc.com/videos/news/eye-catching/vmxJr https://www.abc.com/ap/top/45/state-political-news/fgdfgd
and not match
https://www.abc.com/ap/ap/top-news/ https://www.abc.com/
There should be some value after three "/"'s after the domain name and should match

Replies are listed 'Best First'.
Re: match the urls
by tobyink (Canon) on Jan 11, 2013 at 09:38 UTC
    use 5.010; use strict; use warnings; my @urls = qw< https://www.abc.com/ap/ap/top-news/76/nTk3M/ https://www.abc.com/videos/news/eye-catching/vmxJr https://www.abc.com/ap/top/45/state-political-news/fgdfgd https://www.abc.com/ap/ap/top-news/ https://www.abc.com/ >; for (@urls) { m{ ^ https://www\.abc\.com/ # correct stem (?: .+? / ){3} # non-greedy string then slash x 3 .+ # at least one other character }x ? say("MATCH: $_") : say("NOT: $_") }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: match the urls
by Anonymous Monk on Jan 11, 2013 at 09:25 UTC
Re: match the urls
by sen (Hermit) on Jan 11, 2013 at 14:35 UTC
    #!/usr/bin/perl use strict; use warnings; my @array = ('https://www.abc.com/ap/ap/top-news/76/nTk3M/','https://w +ww.abc.com/videos/news/eye-catching/vmxJr','https://www.abc.com/ap/to +p/45/state-politic al-news/fgdfgd', 'https://www.abc.com/ap/ap/top-news/', 'https://www.a +bc.com/'); my @b = grep {$_ =~ /\w+\:\/\/\w+\.\w+\.\w+\/(\w+\-?\w+?\/){3,}./ } @a +rray; print "@b";