echo "$(expr "title: Purple Haze artist: Jimi Hendrix" : 'title:\s*\(.*\?\)\s*artist.*' )"
prints
Purple Haze
With the trailing whitespace, even though I am using the ? lazy operator.
I've tested this on https://regex101.com/ and it works as expected, what's different about bash?
You aren't using bash's regexp matching, you're using expr. expr does not have a “? lazy operator”, it only implements basic regular expressions (with a few extensions in the Linux version, such as \s for whitespace, but that doesn't include Perl-like lazy operators). (Neither does bash, for that matter.)
If you don't want .* to include trailing space, specify that it must end with a character that isn't a space:
'title:\s*\(.*\S\)\s*artist.*'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With