in reply to Capturing substrings with complex delimiter, up to a maximum

My proposal is to first split and capture on http:// or https:// with an optional comma beforehand and then put back together:

sub getNurls { my( $s, $n ) = @_; my @urls = split /,?(https?:\/\/)/, $s; return map { defined $urls[2*$_-1]?$urls[2*$_-1].$urls[2*$_]:() } +(1..$n); } my $max = 2; my $input = q|http://abc.org,http://de,f.org,https://ghi.org|; print "$_\n" for getNurls( $input, $max );

(borrowing notation from prior posts)

Replies are listed 'Best First'.
Re^2: Capturing substrings with complex delimiter, up to a maximum
by tobyink (Canon) on Oct 30, 2013 at 10:17 UTC

    But what if one of the URLs contains "http://"? For example...

    http://translator.example.com/?url=http://www.example.fr/

    Rather than an optional comma, an improvement would be to look for a comma or \A:

    /(?:\A|,)(https?:\/\/)/
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

      That would be a problem. But can we rule out that a URL contains ",http://"?