# Bash
# vim: ft=bash:

re='ab*c'
if [[ "${foo-}" =~ $re ]]; then blabla; fi

# POSIX

ere_match() { awk -- 'BEGIN { exit !(ARGV[1] ~ ARGV[2]) }' "$@"; }

if ere_match "${foo-}" "${re-}"; then blabla; fi

# With expr, leading anchors are implied. An initial .* works around this. We
# also need to prefix the subject with a character or string not starting with -
# and that is not found at the start of any expr operator present or future.

if expr "@${foo}" : "@.*${re}" >/dev/null; then blabla; fi

# Grep can only be used for single-line strings, but can do case insensitive matching
# with -i. -x can be used to anchor at both start or end or use the usual ^ or $

if printf '%s\n' "${foo}" | grep -q -- "${re}"; then blabla; fi

# matching an email address

read -p "Enter email: " email

if [[ "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$ ]]; then
    echo "This email address looks fine: $email"
else
    echo "This email address is flawed: $email"
fi

# accessing the match groups

if [[ "$thing" =~ ^(.+): (.+)$ ]]; then
  echo "key: ${BASH_REMATCH[1]}"
  echo "val: ${BASH_REMATCH[2]}"
fi
