Your question isn't very well posed, because your splitting should show "A quick "
(with a trailing space). You don't even tell how you want the output to be given (in an array, in two separate variables...). Never, let me rephrase your question in my way, and answer this rephrased question. If it's not what you want, you'll know how to modify your original post.
Given a string s
and a regex var
, give an array a
with two fields a[0]
and a[1]
such that the concatenation of the two fields of array a
is s
and such that a[1] =~ ^var.*
with a[1]
being the minimal (non-greedy) match.
Well, this already exists in bash:
[[ $s =~ ^(.*)($var.*)$ ]]
a=( "${BASH_REMATCH[@]:1}" )
Look:
$ s="A quick brown fox"
$ var=brown
$ [[ $s =~ ^(.*)($var.*)$ ]]
$ a=( "${BASH_REMATCH[@]:1}" )
$ declare -p a
declare -a a='([0]="A quick " [1]="brown fox")'
$