This article shous you how to use regular expression in Bash
tomatch='/php-status?full&json'
[[ "$tomatch" =~ '/php-status' ]] && echo match || echo does not match
[[ ! "$tomatch" =~ 'not' ]] && echo does not match || echo does match
This example shows you how you can capture a Group to extract text.
Example where we will extract a part of a file name
fileName="helloWorld.jpg"
# The regular expression starts with $' and ends with '
regex=$'(.*)\.jpg'
# double bracket is mandatory to use the regular expression operators
if [[ $fileName =~ $regex ]]
then
name="${BASH_REMATCH[1]}"
echo ${name}
else
echo "$fileName doesn't match"
fi
When the regexp binary operator =~ is used, the string to the right of the operator is considered an extended regular expression and matched accordingly.
The operator has the same precedence as == and !=
The return value is:
Others:
Note that pathname expansion use globbing as well as the control flow structure such as case