Text between: Difference between revisions

Added Perl example
m (→‎{{header|Ring}}: Remove vanity tags)
(Added Perl example)
Line 1,315:
>textBetween("FooBarBazFooBuxQuux", "Foo", "Foo");
"BarBaz"</pre>
 
=={{header|Perl}}==
{{trans|Perl 6}}
<lang perl>use feature 'say'
 
sub text_between {
my($text, $start, $end) = @_;
return join ',', $text =~ /$start(.*?)$end/g;
}
 
$text = 'Hello Rosetta Code world';
 
# String start and end delimiter
say '1> '. text_between($text, 'Hello ', ' world' );
 
# Regex string start delimiter
say '2> '. text_between($text, qr/^/, ' world' );
 
# Regex string end delimiter
say '3> '. text_between($text, 'Hello ', qr/$/ );
 
# End delimiter only valid after start delimiter
say '4> '. text_between('</div><div style="chinese">你好嗎</div>', '<div style="chinese">', '</div>' );
 
# End delimiter not found, default to string end
say '5> '. text_between('<text>Hello <span>Rosetta Code</span> world</text><table style="myTable">', '<text>', qr/<table>|$/ );
 
# Start delimiter not found, return blank string
say '6> '. text_between('<table style="myTable"><tr><td>hello world</td></tr></table>', '<table>', '</table>' );
 
# Multiple end delimiters, match frugally
say '7> '. text_between( 'The quick brown fox jumps over the lazy other fox', 'quick ', ' fox' );
 
# Multiple start delimiters, match frugally
say '8> '. text_between( 'One fish two fish red fish blue fish', 'fish ', ' red' );
 
# Start delimiter is end delimiter
say '9> '. text_between('FooBarBazFooBuxQuux', 'Foo', 'Foo' );
 
# Return all matching strings when multiple matches are possible
say '10> '. text_between( $text, 'e', 'o' );
 
# Ignore start and end delimiter string embedded in longer words
$text = 'Soothe a guilty conscience today, string wrangling is not the best tool to use for this job.';
say '11> '. text_between($text, qr/\bthe /, qr/ to\b/);</lang>
{{out}}
<pre>1> Rosetta Code
2> Hello Rosetta Code
3> Rosetta Code world
4> 你好嗎
5> Hello <span>Rosetta Code</span> world</text><table style="myTable">
6>
7> brown
8> two fish
9> BarBaz
10> ll,tta C, w
11> best tool</pre>
 
=={{header|Perl 6}}==
Line 1,320 ⟶ 1,377:
It seems somewhat pointless to write a special purpose routine to do text matching as built-in primitives can do so more flexibly and concisely, but whatever.
 
This version doesn't use strings for meta indexes ('start' and 'end'), rather it accepts regex assertions which are parsed differently from strings. This allows much more robust and fine grained control over what does and doesn't match. (and allows delimiter strings of 'start' and 'end' incidentally.) See the 11th example below which will confound nearly all of the current string -only based implementations.
 
<lang perl6>sub text-between ( $text, $start, $end ) {
2,392

edits