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

Combine split with limit and lookahead-assertion!

DB<107> $str=join ",",@captures => "http://abc.org,http://de,f.org,https://ghi.org" DB<108> split /,(?=https?:)/,$str,1 => "http://abc.org,http://de,f.org,https://ghi.org" DB<109> split /,(?=https?:)/,$str,2 => ("http://abc.org", "http://de,f.org,https://ghi.org") DB<110> split /,(?=https?:)/,$str,3 => ("http://abc.org", "http://de,f.org", "https://ghi.org")

you just need to get rid of the unsplitted rest (if any)

DB<122> $n=1 => 1 DB<123> @array[0..$n-1] = split /,(?=https?:)/ , $str , $n+1 => "http://abc.org" DB<124> $n=2 => 2 DB<125> @array[0..$n-1] = split /,(?=https?:)/ , $str , $n+1 => ("http://abc.org", "http://de,f.org")

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: Capturing substrings with complex delimiter, up to a maximum
by jkeenan1 (Deacon) on Oct 30, 2013 at 01:33 UTC

    Thank you very much for your rapid response. As was the case with BrowserUK's response, I tried to adapt your suggestion to a subroutine I could easily drop into my test program.

    sub _rolf_recognize_limited_urls { my ($input, $max) = @_; my @captures = (); @captures[0..$max-1] = split /,(?=https?:)/, $input, $max+1; return [ @captures ]; }

    As was the case with BrowserUK's formulation, the first two of four tests failed. I only got all four tests to past when I grepped for definedness:

    sub _rolf_recognize_limited_urls { my ($input, $max) = @_; my @captures = (); @captures[0..$max-1] = split /,(?=https?:)/, $input, $max+1; return [ grep { defined($_) } @captures ]; }

    Thank you very much.

    Jim Keenan