Sub-string Extractor with Specific Keywords
JavaScript (ES6), 80 75 bytes
This contains some unprintable characters which are escaped below.
(s,a,b)=>s.replace(b||/$/,"").replace(a,"").match(/ *(.*?) *|$/)[1]||""
Try it online!
Commented
(s, a, b) => // s = input string, a = start keyword, b = end keyword
s.replace( // replace in s:
b || /$/, // look for the end keyword, or the regex /$/ if it's empty
"\3" // and replace it with ETX (end of text)
) //
.replace( // replace in the resulting string:
a, // look for the start keyword
"\2" // and replace it with STX (start of text)
) //
.match( // attempt to match:
/\2 *(.*?) *\3|$/ // "\2" STX
) // " *" followed by optional whitespace
// "(.*?)" followed by a non-greedy string (the payload)
// " *" followed by optional whitespace
// "\3" followed by ETX
// "|$" OR match an empty string to make sure that
// match() doesn't return null
[1] || "" // return the payload string, or an empty string if undefined
Python 3, 86 77 75 bytes
Saved 9 bytes thanks to movatica!!!
Saved 2 bytes thanks to ovs!!!
lambda s,a,b:s[s.find(a):(b in s)*s.find(b)if b else None][len(a):].strip()
Try it online!
APL (Dyalog Extended), 24 bytes (SBCS)
Full program that prompts for array of [EndKeyword,StartKeyword,InputString]
. Requires 0-based indexing.
⌂deb⊃(⌽⊢↓⍨1⍳⍨⊣,⍷)/⌽¨@0⊢⎕
Try it online!
⎕
prompt for input
⊢
on that…
⌽¨@0
reverse all the elements that occur at offset 0
(
…)/
reduce from the right using the following tacit function:
⍷
indicate with a Boolean list all the places where the left argument begins in the right argument
⊣,
prepend the left argument to that
1⍳⍨
find the offset of the first 1
⊢↓⍨
drop that many leading elements from the right argument
⌽
reverse (next time around, do this from the end, and after that, revert order)
⊃
disclose the enclosure caused by the reduction from a 1-dimensional array to a 0-dimensional array
⌂deb
delete ending (leading and trailing) blanks