Jump to content

Text between: Difference between revisions

→‎{{header|Lua}}: added Lua solution
No edit summary
(→‎{{header|Lua}}: added Lua solution)
Line 1,964:
Output: "BarBaz"
</pre>
 
=={{header|Lua}}==
French (instead of Chinese) in #4 to avoid off-topic unicode complications. Coded to satisfy this task as worded, though in actual use reader might be better served by directly applying native pattern matching facilities to specific problems.
<lang lua>function textbetween(text, sdelim, edelim)
-- case #5 (end delimiter not present) is only problem for simplest approach, so preprocess:
if not text:find(edelim=="end" and "$" or edelim) then edelim = "end" end
-- then just:
local re = (sdelim=="start" and "^" or sdelim) .. "(.-)" .. (edelim=="end" and "$" or edelim)
return text:match(re) or ""
end
 
function test(text, sdelim, edelim, expected)
print(textbetween(text, sdelim, edelim) == expected)
end
 
test( "Hello Rosetta Code world", "Hello ", " world", "Rosetta Code" )
test( "Hello Rosetta Code world", "start", " world", "Hello Rosetta Code" )
test( "Hello Rosetta Code world", "Hello ", "end", "Rosetta Code world" )
test( "</div><div style=\"french\">bonjour</div>", "<div style=\"french\">", "</div>", "bonjour" )
test( "<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">", "<text>", "<table>", "Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">" )
test( "<table style=\"myTable\"><tr><td>hello world</td></tr></table>", "<table>", "</table>", "" )
test( "The quick brown fox jumps over the lazy other fox", "quick ", " fox", "brown" )
test( "One fish two fish red fish blue fish", "fish ", " red", "two fish" )
test( "FooBarBazFooBuxQuux", "Foo", "Foo", "BarBaz" )</lang>
{{Out}}
All tests pass, output not given.
 
=={{header|Maple}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.