C:\test>p1 sub getNurls{ my($s,$n) = @_; my @urls = $s =~ m[(https?://.+?)(?:,(?=http)|$)]g; return @urls[ 0 .. $n-1 ]; };; @raw_inputs = ( 'http://abc.org', 'http://de,f.org', 'https://ghi.org', 'http://jkl.org', );; $s = join ',', @raw_inputs;; print for getNurls( $s, 4 );; http://abc.org http://de,f.org https://ghi.org http://jkl.org print for getNurls( $s, 3 );; http://abc.org http://de,f.org https://ghi.org print for getNurls( $s, 2 );; http://abc.org http://de,f.org print for getNurls( $s, 1 );; http://abc.org