Text between: Difference between revisions

no edit summary
(→‎{{header|Lua}}: added Lua solution)
No edit summary
Line 3,068:
text_between("One fish two fish red fish blue fish", "fish ", " red") = "two fish"
text_between("FooBarBazFooBuxQuux", "Foo", "Foo") = "BarBaz"</pre>
 
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
<lang SNOBOL4>
* Program: text_between.sbl
* To run: sbl -r text_between.sbl
* Description: Get the text in a string that occurs between
* a start and end delimiter. Programs will be given a search string,
* a start delimiter string, and an end delimiter string. The delimiters
* will not be unset, and will not be the empty string.
* Comment: Tested using the Spitbol for Linux version of SNOBOL4
 
lf = substr(&alphabet,11,1) ;* New line or line feed
 
 
* Function text_between will return the text between start and end delimiters,
* where start can be the word 'start' for the beginning of the text,
* and end can be the word 'end' for the end of the text.
define('text_between(text,start,end)sb,eb')
:(text_between_end)
text_between
sb = (ident(start,'start') pos(0), breakx(substr(start,1,1)) start)
eb = (ident(end,'end') rem . text_between, (arb . text_between end) | (rem . text_between) )
text ? sb eb
:(return)
text_between_end
 
 
* Read text lines after the END statement
in1
line = input :f(in1end)
line ? break('|') . text '|' break('|') . start '|' rem . end
output = lf 'Text: "' text '"'
output = 'Start: "' start '"'
output = 'End: "' end '"'
text_between = text_between(text,start,end)
output = 'Output: "' text_between '"'
:(in1)
in1end
 
END
Hello Rosetta Code world|Hello | world
Hello Rosetta Code world|start| world
Hello Rosetta Code world|Hello |end
</div><div style=\"chinese\">你好嗎</div>|<div style=\"chinese\">|</div>
<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">|<text>|<table>
<table style=\"myTable\"><tr><td>hello world</td></tr></table>|<table>|</table>
The quick brown fox jumps over the lazy other fox|quick | fox
One fish two fish red fish blue fish|fish | red
FooBarBazFooBuxQuux|Foo|Foo
</lang>
{{out}}
<pre>
Text: "Hello Rosetta Code world"
Start: "Hello "
End: " world"
Output: "Rosetta Code"
 
Text: "Hello Rosetta Code world"
Start: "start"
End: " world"
Output: "Hello Rosetta Code"
 
Text: "Hello Rosetta Code world"
Start: "Hello "
End: "end"
Output: "Rosetta Code world"
 
Text: "</div><div style=\"chinese\">你好嗎</div>"
Start: "<div style=\"chinese\">"
End: "</div>"
Output: "你好嗎"
 
Text: "<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">"
Start: "<text>"
End: "<table>"
Output: "Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">"
 
Text: "<table style=\"myTable\"><tr><td>hello world</td></tr></table>"
Start: "<table>"
End: "</table>"
Output: ""
 
Text: "The quick brown fox jumps over the lazy other fox"
Start: "quick "
End: " fox"
Output: "brown"
 
Text: "One fish two fish red fish blue fish"
Start: "fish "
End: " red"
Output: "two fish"
 
Text: "FooBarBazFooBuxQuux"
Start: "Foo"
End: "Foo"
Output: "BarBaz"
</pre>
 
=={{header|Swift}}==
Anonymous user