Regex → Bash
//g
grep -P (PCRE) is only available on GNU grep. macOS users may need ggrep (brew install grep) or fall back to -E with POSIX ERE syntax.
Bash / grep / sed / awk
#!/usr/bin/env bash
input='Reach alice@example.com or bob@test.org.'
pattern='\\b\\w+@\\w+\\.\\w+\\b'
# Match (POSIX ERE via grep)
if echo "$input" | grep -E '\\b\\w+@\\w+\\.\\w+\\b' > /dev/null; then
echo "matched"
fi
# First match only (PCRE — requires GNU grep with -P)
first=$(echo "$input" | grep -Po '\\b\\w+@\\w+\\.\\w+\\b' | head -n1)
echo "first=$first"
# All matches, one per line
echo "$input" | grep -Po '\\b\\w+@\\w+\\.\\w+\\b'
# Replace with sed (ERE)
replaced=$(echo "$input" | sed -E 's/\\b\\w+@\\w+\\.\\w+\\b/[email]/g')
echo "replaced=$replaced"
# Split (treat pattern as the separator, output one field per line)
echo "$input" | awk -v RS='\\b\\w+@\\w+\\.\\w+\\b' '{ print }'
# Bash built-in =~ (POSIX ERE, no flags)
if [[ "$input" =~ \\b\\w+@\\w+\\.\\w+\\b ]]; then
echo "bash match: ${BASH_REMATCH[0]}"
fi