I'm attempting to create a regex that captures both the HTTP status code as well as the body of a curl request. The regex pattern below works on multiple online sites, but won't match in a shell if-statement on my Mac's command line. Is my regex off or is there something else going on?
RESPONSE=$(curl -s -i -X GET http://www.google.com/)
# Match and capture the status code, match the headers, match two new lines, match and capture an optional body
re="^HTTP\/\d\.\d\s([\d]{3})[\w\d\s\W\D\S]*[\r\n]{2}([\w\d\s\W\D\S]*)?$"
if [[ "${RESPONSE}" =~ $re ]]; then
echo "match"
# Now do stuff with the captured groups, "${BASH_REMATCH[...]}"
else
echo "no match"
fi
I'm also open to other ways of doing this (I'm targeting a machine running CentOS 5).