Text between: Difference between revisions
→{{header|Perl 6}}: Add a Perl 6 example
Walterpachl (talk | contribs) (add REXX) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
||
Line 380:
text between 'Goodbye' and 'Code world' is 'Hello Rosetta Code world'
</pre>
=={{header|Perl 6}}==
{{works with|Rakudo|2017.12}}
It seems somewhat pointless to write a general purpose routine to do text matching as built-in primitives can do so more flexibly and concisely, but whatever.
<lang perl6>sub text-between ( $text, $start, $end ) {
return $/»[0]».Str if $text ~~ m:g/ $start (.*?) $end /;
[]
}
# Testing
my $text = 'Hello Rosetta Code world';
# String start and end deliminter
put '1> ', $text.&text-between( 'Hello ', ' world' );
# Regex string start delimiter
put '2> ', $text.&text-between( rx/^/, ' world' );
# Regex string end delimiter
put '3> ', $text.&text-between( 'Hello', rx/$/ );
# Return all matching strings when multiple matches are possible
put '4> ', join ',', $text.&text-between( 'e', 'o' );
# End delimiter only valid after start delimiter
put '5> ', '</div><div style="chinese">你好嗎</div>'\
.&text-between( '<div style="chinese">', '</div>' );
# End deliminitor not found, default to string end
put '6> ', '<text>Hello <span>Rosetta Code</span> world</text><table style="myTable">'\
.&text-between( '<text>', rx/'<table>' | $/ );
# Start delimiter not found, return blank string
put '7> ', '<table style="myTable"><tr><td>hello world</td></tr></table>'\
.&text-between( '<table>', '</table>' );</lang>
{{out}}
<pre>1> Rosetta Code
2> Hello Rosetta Code
3> Rosetta Code world
4> ll,tta C, w
5> 你好嗎
6> Hello <span>Rosetta Code</span> world</text><table style="myTable">
7> </pre>
=={{header|PHP}}==
|