Text between: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 92: Line 92:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F textBetween(thisText, startString, endString)
<syntaxhighlight lang="11l">F textBetween(thisText, startString, endString)
X.try
X.try
Int startIndex
Int startIndex
Line 127: Line 127:
V startString = :argv[2]
V startString = :argv[2]
V endString = :argv[3]
V endString = :argv[3]
print(textBetween(thisText, startString, endString))</lang>
print(textBetween(thisText, startString, endString))</syntaxhighlight>


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>BYTE FUNC Matches(CHAR ARRAY text,sub BYTE index)
<syntaxhighlight lang="action!">BYTE FUNC Matches(CHAR ARRAY text,sub BYTE index)
CHAR ARRAY tmp(255)
CHAR ARRAY tmp(255)


Line 200: Line 200:
Test(8,"One fish two fish red fish blue fish","fish "," red")
Test(8,"One fish two fish red fish blue fish","fish "," red")
Test(9,"FooBarBazFooBuxQuux","Foo","Foo")
Test(9,"FooBarBazFooBuxQuux","Foo","Foo")
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Text_between.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Text_between.png Screenshot from Atari 8-bit computer]
Line 224: Line 224:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_Io;
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Strings.Fixed;
with Ada.Strings.Fixed;


Line 289: Line 289:
Test_Between ("One fish two fish red fish blue fish", First => "fish ", Last => " red");
Test_Between ("One fish two fish red fish blue fish", First => "fish ", Last => " red");
Test_Between ("FooBarBazFooBuxQuux", First => "Foo", Last => "Foo");
Test_Between ("FooBarBazFooBuxQuux", First => "Foo", Last => "Foo");
end Text_Between;</lang>
end Text_Between;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 342: Line 342:
Uses the Algol 68G specific string in string, for other compilers/interpreters, a version of string in string is here : [[ALGOL_68/prelude]].<br/>
Uses the Algol 68G specific string in string, for other compilers/interpreters, a version of string in string is here : [[ALGOL_68/prelude]].<br/>
As Algol 68 predates Unicode, the fourth example deviates from the task.
As Algol 68 predates Unicode, the fourth example deviates from the task.
<lang algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
# some utility operators #
# some utility operators #
# returns the length of a string #
# returns the length of a string #
Line 412: Line 412:
trace between := FALSE
trace between := FALSE
END
END
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 463: Line 463:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>
<syntaxhighlight lang="applescript">
my text_between("Hello Rosetta Code world", "Hello ", " world")
my text_between("Hello Rosetta Code world", "Hello ", " world")


Line 485: Line 485:
return return_text
return return_text
end text_between
end text_between
</syntaxhighlight>
</lang>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>data =
<syntaxhighlight lang="autohotkey">data =
(
(
Hello Rosetta Code world|Hello | world|
Hello Rosetta Code world|Hello | world|
Line 510: Line 510:
RegExMatch(text,(start="start"?"^":"\Q" start "\E") "(.*?)" (end="end"?"$":"\Q" end "\E?"),m)
RegExMatch(text,(start="start"?"^":"\Q" start "\E") "(.*?)" (end="end"?"$":"\Q" end "\E?"),m)
return m1
return m1
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>text: "Hello Rosetta Code world"
<pre>text: "Hello Rosetta Code world"
Line 550: Line 550:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f TEXT_BETWEEN.AWK
# syntax: GAWK -f TEXT_BETWEEN.AWK
BEGIN {
BEGIN {
Line 600: Line 600:
printf("Output: '%s'\n\n",str)
printf("Output: '%s'\n\n",str)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 677: Line 677:


=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang="c">
<lang c>
/*
/*
* textBetween: Gets text between two delimiters
* textBetween: Gets text between two delimiters
Line 736: Line 736:
return startPointer;
return startPointer;
} // end textBetween method</lang>
} // end textBetween method</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|D}}
{{trans|D}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;


namespace TextBetween {
namespace TextBetween {
Line 785: Line 785:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
{{trans|C#}}
{{trans|C#}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>


std::ostream& operator<<(std::ostream& out, const std::string& str) {
std::ostream& operator<<(std::ostream& out, const std::string& str) {
Line 835: Line 835:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>text: 'Hello Rosetta Code world'
<pre>text: 'Hello Rosetta Code world'
Line 878: Line 878:


=={{header|D}}==
=={{header|D}}==
<lang D>import std.algorithm.searching;
<syntaxhighlight lang="d">import std.algorithm.searching;
import std.stdio;
import std.stdio;
import std.string;
import std.string;
Line 921: Line 921:
print("One fish two fish red fish blue fish", "fish ", " red");
print("One fish two fish red fish blue fish", "fish ", " red");
print("FooBarBazFooBuxQuux", "Foo", "Foo");
print("FooBarBazFooBuxQuux", "Foo", "Foo");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>text: 'Hello Rosetta Code world'
<pre>text: 'Hello Rosetta Code world'
Line 970: Line 970:
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}
{{Trans|C#}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Text_between;
program Text_between;


Line 1,036: Line 1,036:


Readln;
Readln;
end.</lang>
end.</syntaxhighlight>
=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: combinators formatting kernel locals math
<syntaxhighlight lang="factor">USING: combinators formatting kernel locals math
prettyprint.config sequences ;
prettyprint.config sequences ;
IN: rosetta-code.text-between
IN: rosetta-code.text-between
Line 1,080: Line 1,080:
] each ;
] each ;


MAIN: text-between-demo</lang>
MAIN: text-between-demo</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,132: Line 1,132:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|VBA}}
{{trans|VBA}}
<lang freebasic>Const DELIM_INICIO As String = "start"
<syntaxhighlight lang="freebasic">Const DELIM_INICIO As String = "start"
Const DELIM_FINAL As String = "end"
Const DELIM_FINAL As String = "end"


Line 1,215: Line 1,215:
'Resultado:
'Resultado:
Print Salida
Print Salida
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,235: Line 1,235:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,295: Line 1,295:
fmt.Printf("Output: \"%s\"\n\n", b)
fmt.Printf("Output: \"%s\"\n\n", b)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,303: Line 1,303:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>import Control.Monad (join)
<syntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bifunctor (bimap)
import Data.Bifunctor (bimap)
import Data.List (intercalate)
import Data.List (intercalate)
Line 1,371: Line 1,371:
wrap x
wrap x
| x `elem` ["start", "end"] = Left x
| x `elem` ["start", "end"] = Left x
| otherwise = Right (pack x)</lang>
| otherwise = Right (pack x)</syntaxhighlight>
{{Out}}
{{Out}}
<pre>"Rosetta Code"
<pre>"Rosetta Code"
Line 1,382: Line 1,382:
=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<lang j>textBetween=: dyad define
<syntaxhighlight lang="j">textBetween=: dyad define
text=. y
text=. y
'start end'=. x
'start end'=. x
Line 1,388: Line 1,388:
end=. text"_^:('end'&-:) end
end=. text"_^:('end'&-:) end
end taketo start takeafter text
end taketo start takeafter text
)</lang>
)</syntaxhighlight>


'''Example Usage:'''
'''Example Usage:'''
<lang j> ('Hello ';' world') textBetween 'Hello Rosetta Code world'
<syntaxhighlight lang="j"> ('Hello ';' world') textBetween 'Hello Rosetta Code world'
Rosetta Code</lang>
Rosetta Code</syntaxhighlight>


'''Examples:'''
'''Examples:'''
<lang j> Test_text=: <;._2 noun define
<syntaxhighlight lang="j"> Test_text=: <;._2 noun define
Hello Rosetta Code world
Hello Rosetta Code world
Hello Rosetta Code world
Hello Rosetta Code world
Line 1,432: Line 1,432:


Test_output = Test_delim textBetween&.> Test_text
Test_output = Test_delim textBetween&.> Test_text
1 1 1 1 1 1 1 1 1</lang>
1 1 1 1 1 1 1 1 1</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Line 1,440: Line 1,440:
</pre>
</pre>


<lang java>
<syntaxhighlight lang="java">
public class textBetween
public class textBetween
{
{
Line 1,502: Line 1,502:
} // end class TextBetween
} // end class TextBetween
</syntaxhighlight>
</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
===ES5===
===ES5===
<lang javascript>
<syntaxhighlight lang="javascript">
function textBetween(thisText, startString, endString)
function textBetween(thisText, startString, endString)
{
{
Line 1,545: Line 1,545:
return newText;
return newText;
} // end textBetween
} // end textBetween
</syntaxhighlight>
</lang>
===ES6===
===ES6===
{{Trans|Haskell}}
{{Trans|Haskell}}
Composed from a set of generic functions
Composed from a set of generic functions
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 1,736: Line 1,736:
)
)
);
);
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>[
<pre>[
Line 1,750: Line 1,750:


The implementation uses `explode` to ensure arbitrary Unicode will be handled properly.
The implementation uses `explode` to ensure arbitrary Unicode will be handled properly.
<syntaxhighlight lang="jq">
<lang jq>
def textbetween_strings($startdlm; $enddlm):
def textbetween_strings($startdlm; $enddlm):
explode
explode
Line 1,765: Line 1,765:
end
end
| implode;
| implode;
</syntaxhighlight>
</lang>
=== Verification ===
=== Verification ===


<lang>
<syntaxhighlight lang="text">
def testdata:
def testdata:
(["Hello Rosetta Code world", "Hello ", " world"],
(["Hello Rosetta Code world", "Hello ", " world"],
Line 1,785: Line 1,785:
| $in[0]
| $in[0]
| textbetween_strings($in[1]; $in[2])
| textbetween_strings($in[1]; $in[2])
</syntaxhighlight>
</lang>
===Output===
===Output===
<lang>
<syntaxhighlight lang="text">
"Rosetta Code"
"Rosetta Code"
"Hello Rosetta Code"
"Hello Rosetta Code"
Line 1,797: Line 1,797:
"two fish"
"two fish"
"BarBaz"
"BarBaz"
</syntaxhighlight>
</lang>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<lang julia>function textbetween(text::AbstractString, startdlm::AbstractString, enddlm::AbstractString)
<syntaxhighlight lang="julia">function textbetween(text::AbstractString, startdlm::AbstractString, enddlm::AbstractString)
startind = startdlm != "start" ? last(search(text, startdlm)) + 1 : 1
startind = startdlm != "start" ? last(search(text, startdlm)) + 1 : 1
endind = enddlm != "end" ? first(search(text, enddlm, startind)) - 1 : endof(text)
endind = enddlm != "end" ? first(search(text, enddlm, startind)) - 1 : endof(text)
Line 1,822: Line 1,822:
for (text, s, e) in testcases
for (text, s, e) in testcases
println("\nText: ", text, "\nStart delim: ", s, "\nEnd delim: ", e, "\nOutput: ", textbetween(text, s, e))
println("\nText: ", text, "\nStart delim: ", s, "\nEnd delim: ", e, "\nOutput: ", textbetween(text, s, e))
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,872: Line 1,872:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
In the third example, I've assumed that the start delimiter should be "Hello " (not "Hello") to match the required output.
In the third example, I've assumed that the start delimiter should be "Hello " (not "Hello") to match the required output.
<lang scala>// version 1.2.10
<syntaxhighlight lang="scala">// version 1.2.10


fun String.textBetween(start: String, end: String): String {
fun String.textBetween(start: String, end: String): String {
Line 1,916: Line 1,916:
println("Output: \"$b\"\n")
println("Output: \"$b\"\n")
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,967: Line 1,967:
=={{header|Lua}}==
=={{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.
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)
<syntaxhighlight lang="lua">function textbetween(text, sdelim, edelim)
-- case #5 (end delimiter not present) is only problem for simplest approach, so preprocess:
-- 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
if not text:find(edelim=="end" and "$" or edelim) then edelim = "end" end
Line 1,987: Line 1,987:
test( "The quick brown fox jumps over the lazy other fox", "quick ", " fox", "brown" )
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( "One fish two fish red fish blue fish", "fish ", " red", "two fish" )
test( "FooBarBazFooBuxQuux", "Foo", "Foo", "BarBaz" )</lang>
test( "FooBarBazFooBuxQuux", "Foo", "Foo", "BarBaz" )</syntaxhighlight>
{{Out}}
{{Out}}
All tests pass, output not given.
All tests pass, output not given.


=={{header|Maple}}==
=={{header|Maple}}==
<lang Maple>textBetween := proc(str,delim1,delim2)
<syntaxhighlight lang="maple">textBetween := proc(str,delim1,delim2)
local on, off,extra:
local on, off,extra:
on := piecewise(delim1="start", 1, SearchText(delim1, str)):
on := piecewise(delim1="start", 1, SearchText(delim1, str)):
Line 2,000: Line 2,000:
if off <> 0 then off := off+on+extra-1: end if:
if off <> 0 then off := off+on+extra-1: end if:
return str[on+extra..off-1]:
return str[on+extra..off-1]:
end proc:</lang>
end proc:</syntaxhighlight>
{{Out|Examples}}
{{Out|Examples}}
<pre>>textBetween("Hello Rosetta Code world", "Hello ", " world");
<pre>>textBetween("Hello Rosetta Code world", "Hello ", " world");
Line 2,022: Line 2,022:


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>textBetween = function(s, startDelim, endDelim)
<syntaxhighlight lang="miniscript">textBetween = function(s, startDelim, endDelim)
startPos = s.indexOf(startDelim) + startDelim.len
startPos = s.indexOf(startDelim) + startDelim.len
if startDelim == "start" then startPos = 0
if startDelim == "start" then startPos = 0
Line 2,034: Line 2,034:
print textBetween("Hello Rosetta Code world", "Hello ", "end")
print textBetween("Hello Rosetta Code world", "Hello ", "end")
print textBetween("The quick brown fox jumps over the lazy other fox", "quick ", " fox")
print textBetween("The quick brown fox jumps over the lazy other fox", "quick ", " fox")
print textBetween("FooBarBazFooBuxQuux", "Foo", "Foo")</lang>
print textBetween("FooBarBazFooBuxQuux", "Foo", "Foo")</syntaxhighlight>


{{out}}
{{out}}
Line 2,045: Line 2,045:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils
<syntaxhighlight lang="nim">import strutils


func textBetween(text, startStr, endStr: string): string =
func textBetween(text, startStr, endStr: string): string =
Line 2,087: Line 2,087:
echo "End: ", endStr.quote
echo "End: ", endStr.quote
echo "Output: ", text.textBetween(startStr, endStr).quote
echo "Output: ", text.textBetween(startStr, endStr).quote
echo()</lang>
echo()</syntaxhighlight>


{{out}}
{{out}}
Line 2,137: Line 2,137:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|Java}}
{{trans|Java}}
<lang objeck>class TextBetween {
<syntaxhighlight lang="objeck">class TextBetween {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
if(args->Size() = 3) {
if(args->Size() = 3) {
Line 2,178: Line 2,178:
return thisText->SubString(startIndex, endIndex - startIndex);
return thisText->SubString(startIndex, endIndex - startIndex);
}
}
}</lang>
}</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use feature 'say';
<syntaxhighlight lang="perl">use feature 'say';


sub text_between {
sub text_between {
Line 2,223: Line 2,223:
# Ignore start and end delimiter string embedded in longer words
# 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.';
$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>
say '11> '. text_between($text, qr/\bthe /, qr/ to\b/);</syntaxhighlight>
{{out}}
{{out}}
<pre>1> Rosetta Code
<pre>1> Rosetta Code
Line 2,238: Line 2,238:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">text_between</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">start_delimiter</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">end_delimiter</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">text_between</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">start_delimiter</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">end_delimiter</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">start_delimiter</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">"start"</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">start_delimiter</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">"start"</span> <span style="color: #008080;">then</span>
Line 2,283: Line 2,283:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
All tests pass, so no output.
All tests pass, so no output.
Line 2,292: Line 2,292:
</pre>
</pre>


<lang php>
<syntaxhighlight lang="php">
<?php
<?php
function text_between($string, $start, $end)
function text_between($string, $start, $end)
Line 2,338: Line 2,338:
print_r($returnText);
print_r($returnText);
?>
?>
</syntaxhighlight>
</lang>


=={{header|PowerBASIC}}==
=={{header|PowerBASIC}}==
<lang powerbasic>#COMPILE EXE
<syntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
#DIM ALL
#COMPILER PBCC 6
#COMPILER PBCC 6
Line 2,460: Line 2,460:
CON.PRINT IIF$(TextBetween(sText, StartDelim, EndDelim) = Expected, "OK", "failed")
CON.PRINT IIF$(TextBetween(sText, StartDelim, EndDelim) = Expected, "OK", "failed")


END FUNCTION</lang>
END FUNCTION</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang="python">
#!/usr/bin/env python
#!/usr/bin/env python
from sys import argv
from sys import argv
Line 2,509: Line 2,509:


print textBetween( thisText, startString, endString )
print textBetween( thisText, startString, endString )
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require (prefix-in 13: srfi/13))
(require (prefix-in 13: srfi/13))


Line 2,544: Line 2,544:
(test-case "The quick brown fox jumps over the lazy other fox" "quick " " fox" "brown")
(test-case "The quick brown fox jumps over the lazy other fox" "quick " " fox" "brown")
(test-case "One fish two fish red fish blue fish" "fish " " red" "two fish")
(test-case "One fish two fish red fish blue fish" "fish " " red" "two fish")
(test-case "FooBarBazFooBuxQuux" "Foo" "Foo" "BarBaz"))</lang>
(test-case "FooBarBazFooBuxQuux" "Foo" "Foo" "BarBaz"))</syntaxhighlight>
{{out}}
{{out}}
All tests pass, so no output.
All tests pass, so no output.
Line 2,555: Line 2,555:
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.
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 ) {
<syntaxhighlight lang="raku" line>sub text-between ( $text, $start, $end ) {
return $/»[0]».Str if $text ~~ m:g/ $start (.*?) $end /;
return $/»[0]».Str if $text ~~ m:g/ $start (.*?) $end /;
[]
[]
Line 2,601: Line 2,601:
# Ignore start and end delimiter string embedded in longer words
# Ignore start and end delimiter string embedded in longer words
put '11> ', 'Soothe a guilty conscience today, string wrangling is not the best tool to use for this job.'\
put '11> ', 'Soothe a guilty conscience today, string wrangling is not the best tool to use for this job.'\
.&text-between( rx/«'the '/, rx/' to'»/ );</lang>
.&text-between( rx/«'the '/, rx/' to'»/ );</syntaxhighlight>


{{out}}
{{out}}
Line 2,619: Line 2,619:
===version 1===
===version 1===
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang rexx>
<syntaxhighlight lang="rexx">
/*REXX*/
/*REXX*/


Line 2,663: Line 2,663:
End
End


o: Say arg(1) </lang>
o: Say arg(1) </syntaxhighlight>
{{out|Output}}
{{out|Output}}
<pre>Text: "Hello Rosetta Code world"
<pre>Text: "Hello Rosetta Code world"
Line 2,715: Line 2,715:
Also, it wasn't necessary, but I <u>assummed</u> (bad assumption?) that the &nbsp; <big>'''\'''</big> &nbsp; could be an escape character, but unless clarified,
Also, it wasn't necessary, but I <u>assummed</u> (bad assumption?) that the &nbsp; <big>'''\'''</big> &nbsp; could be an escape character, but unless clarified,
<br>it's being treated as a commom character, &nbsp; REXX has no need for escape characters (within character strings).
<br>it's being treated as a commom character, &nbsp; REXX has no need for escape characters (within character strings).
<lang rexx>/*REXX programs displays the text between two text deliminiters in a target text string.*/
<syntaxhighlight lang="rexx">/*REXX programs displays the text between two text deliminiters in a target text string.*/
call TB 'Hello Rosetta Code world', "Hello ", ' world'
call TB 'Hello Rosetta Code world', "Hello ", ' world'
call TB 'Hello Rosetta Code world', "start", ' world'
call TB 'Hello Rosetta Code world', "start", ' world'
Line 2,734: Line 2,734:
if E\=='end' then parse var $ $ (E) /* " " before " END. " */
if E\=='end' then parse var $ $ (E) /* " " before " END. " */
say ' Output: "'$'"' /*display the extracted string to term.*/
say ' Output: "'$'"' /*display the extracted string to term.*/
return</lang>
return</syntaxhighlight>
{{out|output}}
{{out|output}}
<pre>
<pre>
Line 2,784: Line 2,784:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Text between
# Project : Text between


Line 2,830: Line 2,830:
next
next
see '"' + substr(list2str(textdel), nl, " ") +'"' + nl + nl
see '"' + substr(list2str(textdel), nl, " ") +'"' + nl + nl
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,854: Line 2,854:
=={{header|Ruby}}==
=={{header|Ruby}}==
Test
Test
<lang ruby>
<syntaxhighlight lang="ruby">
class String
class String
def textBetween startDelimiter, endDelimiter
def textBetween startDelimiter, endDelimiter
Line 2,900: Line 2,900:


puts returnText
puts returnText
</syntaxhighlight>
</lang>


=={{header|Rust}}==
=={{header|Rust}}==
<syntaxhighlight lang="rust">
<lang Rust>
//Use Into<String> so input can be String, &str or anything else that implements Into<String>
//Use Into<String> so input can be String, &str or anything else that implements Into<String>
fn text_between<S: Into<String>>(input: S, start: S, end: S) -> String {
fn text_between<S: Into<String>>(input: S, start: S, end: S) -> String {
Line 2,942: Line 2,942:
println!("'{}'", text_between("FooBarBazFooBuxQuux", "Foo", "Foo"));
println!("'{}'", text_between("FooBarBazFooBuxQuux", "Foo", "Foo"));
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,957: Line 2,957:


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>object TextBetween extends App {
<syntaxhighlight lang="scala">object TextBetween extends App {
val (thisText, startDelimiter, endDelimiter) = (args(0), args(1),args(2))
val (thisText, startDelimiter, endDelimiter) = (args(0), args(1),args(2))


Line 2,983: Line 2,983:
println(textBetween(thisText, startDelimiter, endDelimiter))
println(textBetween(thisText, startDelimiter, endDelimiter))


}</lang>
}</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
Uses /^/ and /$/ as start and end delimiters. Additionally, the start and end delimiters can be regular expressions.
Uses /^/ and /$/ as start and end delimiters. Additionally, the start and end delimiters can be regular expressions.
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>func text_between (text, beg, end) {
<syntaxhighlight lang="ruby">func text_between (text, beg, end) {


beg.escape! if beg.kind_of(String)
beg.escape! if beg.kind_of(String)
Line 3,057: Line 3,057:
assert_eq(t{:out}, r)
assert_eq(t{:out}, r)
say "text_between(#{t{:text}.dump}, #{t{:start}.dump}, #{t{:end}.dump}) = #{r.dump}"
say "text_between(#{t{:text}.dump}, #{t{:start}.dump}, #{t{:end}.dump}) = #{r.dump}"
}</lang>
}</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
text_between("Hello Rosetta Code world", "Hello ", " world") = "Rosetta Code"
text_between("Hello Rosetta Code world", "Hello ", " world") = "Rosetta Code"
Line 3,071: Line 3,071:
=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
{{works with|SNOBOL4, SPITBOL for Linux}}
<syntaxhighlight lang="snobol4">
<lang SNOBOL4>
* Program: text_between.sbl
* Program: text_between.sbl
* To run: sbl -r text_between.sbl
* To run: sbl -r text_between.sbl
Line 3,118: Line 3,118:
One fish two fish red fish blue fish|fish | red
One fish two fish red fish blue fish|fish | red
FooBarBazFooBuxQuux|Foo|Foo
FooBarBazFooBuxQuux|Foo|Foo
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,169: Line 3,169:
=={{header|Swift}}==
=={{header|Swift}}==


<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


public extension String {
public extension String {
Line 3,215: Line 3,215:
print("End delimiter: \"\(end)\"")
print("End delimiter: \"\(end)\"")
print("Text between: \"\(input.textBetween(start, and: end))\"\n")
print("Text between: \"\(input.textBetween(start, and: end))\"\n")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,266: Line 3,266:
=={{header|Tcl}}==
=={{header|Tcl}}==


<lang Tcl>package require Tcl 8.5
<syntaxhighlight lang="tcl">package require Tcl 8.5


proc between {str start end} {
proc between {str start end} {
Line 3,295: Line 3,295:
puts " [format %15s $t]: $v"
puts " [format %15s $t]: $v"
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,356: Line 3,356:
The "hard" assertions when unpacking the arguments to the "text_between" function reflect the assumptions in the requirements for this problem: that null/empty arguments will never be provided. If any empty arguments are given, the interpreter running this function will exit after printing an error. If this function is invoked without a subshell, that will crash the invoking program as well. In practical use, that may not be desirable, in which case the ":?" assertions should be replaced with less harsh conditional-unpack code (e.g. <code>if [ -z "${1:-}" ]; then echo "Invalid input!" && return 127; else local var="$1"; fi</code>).
The "hard" assertions when unpacking the arguments to the "text_between" function reflect the assumptions in the requirements for this problem: that null/empty arguments will never be provided. If any empty arguments are given, the interpreter running this function will exit after printing an error. If this function is invoked without a subshell, that will crash the invoking program as well. In practical use, that may not be desirable, in which case the ":?" assertions should be replaced with less harsh conditional-unpack code (e.g. <code>if [ -z "${1:-}" ]; then echo "Invalid input!" && return 127; else local var="$1"; fi</code>).


<lang bash>text_between() {
<syntaxhighlight lang="bash">text_between() {
local search="${1:?Search text not provided}"
local search="${1:?Search text not provided}"
local start_str="${2:?Start text not provided}"
local start_str="${2:?Start text not provided}"
Line 3,388: Line 3,388:
text_between "Hello Rosetta Code world" "Hello " " world"
text_between "Hello Rosetta Code world" "Hello " " world"
text_between "Hello Rosetta Code world" "start" " world"
text_between "Hello Rosetta Code world" "start" " world"
text_between "Hello Rosetta Code world" "Hello " "end"</lang>
text_between "Hello Rosetta Code world" "Hello " "end"</syntaxhighlight>


=={{header|VBA}}==
=={{header|VBA}}==
<lang vb>Option Explicit
<syntaxhighlight lang="vb">Option Explicit


Private Const STRING_START As String = "Start"
Private Const STRING_START As String = "Start"
Line 3,479: Line 3,479:
Text_Between = Mid(T, Len(F) + InStr(T, F), InStr(InStr(T, F), T, L) - (Len(F) + InStr(T, F)))
Text_Between = Mid(T, Len(F) + InStr(T, F), InStr(InStr(T, F), T, L) - (Len(F) + InStr(T, F)))
End Select
End Select
End Function</lang>
End Function</syntaxhighlight>
{{out}}
{{out}}
<pre>1- Rosetta Code
<pre>1- Rosetta Code
Line 3,496: Line 3,496:
=={{header|Visual Basic}}==
=={{header|Visual Basic}}==
{{works with|Visual Basic|6}}
{{works with|Visual Basic|6}}
<lang vb>Public Function TextBetween(ByVal Text As String, ByVal StartDelim As String, ByVal EndDelim As String) As String
<syntaxhighlight lang="vb">Public Function TextBetween(ByVal Text As String, ByVal StartDelim As String, ByVal EndDelim As String) As String
Dim indS As Long
Dim indS As Long
Dim indE As Long
Dim indE As Long
Line 3,604: Line 3,604:
Text = StartDelim & Expected & EndDelim & "FooBuxQuux"
Text = StartDelim & Expected & EndDelim & "FooBuxQuux"
Debug.Assert TextBetween(Text, StartDelim, EndDelim) = Expected
Debug.Assert TextBetween(Text, StartDelim, EndDelim) = Expected
End Sub</lang>
End Sub</syntaxhighlight>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1
Function TextBetween(source As String, pre As String, suf As String) As String
Function TextBetween(source As String, pre As String, suf As String) As String
Dim startIndex As Integer
Dim startIndex As Integer
Line 3,651: Line 3,651:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>text: 'Hello Rosetta Code world'
<pre>text: 'Hello Rosetta Code world'
Line 3,700: Line 3,700:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>fn text_between(str string, start string, end string) string {
<syntaxhighlight lang="vlang">fn text_between(str string, start string, end string) string {
if str == "" || start == "" || end == "" {
if str == "" || start == "" || end == "" {
return str
return str
Line 3,753: Line 3,753:
println("Output: \"$b\"\n")
println("Output: \"$b\"\n")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,806: Line 3,806:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var textBetween = Fn.new { |str, start, end|
var textBetween = Fn.new { |str, start, end|
Line 3,851: Line 3,851:
Fmt.print("Output: $q\n", b)
Fmt.print("Output: $q\n", b)
i = i + 1
i = i + 1
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,903: Line 3,903:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|Wren}}
{{trans|Wren}}
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Text_between
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Text_between
// by Galileo, 04/2022
// by Galileo, 04/2022


Line 3,940: Line 3,940:
print "End delimiter: ", end$
print "End delimiter: ", end$
print "Output: ", textBetween$(text$, start$, end$), "\n"
print "Output: ", textBetween$(text$, start$, end$), "\n"
loop</lang>
loop</syntaxhighlight>
{{out}}
{{out}}
<pre>Text: Hello Rosetta Code world
<pre>Text: Hello Rosetta Code world
Line 3,990: Line 3,990:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn getText(text,start,end){
<syntaxhighlight lang="zkl">fcn getText(text,start,end){
s = (if((s:=text.find(start))==Void) 0 else s + start.len());
s = (if((s:=text.find(start))==Void) 0 else s + start.len());
e = (if((e:=text.find(end,s))==Void) text.len() else e);
e = (if((e:=text.find(end,s))==Void) text.len() else e);
Line 3,997: Line 3,997:
getText("Hello Rosetta Code world","Hello "," world").println();
getText("Hello Rosetta Code world","Hello "," world").println();
getText("Hello Rosetta Code world","start", " world").println();
getText("Hello Rosetta Code world","start", " world").println();
getText("Hello Rosetta Code world","Hello", "end" ).println();</lang>
getText("Hello Rosetta Code world","Hello", "end" ).println();</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>