Why does BASH_REMATCH not work for a quoted regular expression?
In your bash REGEX, you should remove quotes. That's why that doesn't work.
If you have space, I recommend to use this way :
#!/bin/bash
x='foo bar bletch'
if [[ $x =~ foo[[:space:]](bar)[[:space:]]bl(.*) ]]
then
echo The regex matches!
echo $BASH_REMATCH
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[2]}
fi
You can also assign the quoted regexp to a variable:
#!/bin/bash
x='foobarbletch'
foobar_re='foo(bar)bl(.*)'
if [[ $x =~ $foobar_re ]] ; then
echo The regex matches!
echo ${BASH_REMATCH[*]}
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[2]}
fi
This not only supports simple quoting but gives the regexp a name which can help readability.