As Gilles points out, you're not using bash regular expressions. To do so, you could use the regex match operator =~
like this:
re='title:[[:space:]]*(.*[^[:space:]])[[:space:]]*artist.*'
details='title: Purple Haze artist: Jimi Hendrix'
[[ $details =~ $re ]] && echo "${BASH_REMATCH[1]}"
Rather than using a lazy match, this uses a non-space character at the end of the capture group, so the trailing space is removed. The first capture group is stored in ${BASH_REMATCH[1]}
.
At the expense of cross-platform portability, it is also possible to use the shorthand \s
and \S
instead of [[:space:]]
and [^[:space:]]
:
re='title:\s*(.*\S)\s*artist.*'