Text Processing and File OperationsLesson 4.5
Bash regex matching and string validation
=~ operator in [[ ]], BASH_REMATCH array, ERE syntax, validating email IP UUID date formats, regex vs glob, anchoring patterns, character classes, negation
Regex in Bash with =~
The =~ operator inside [[ ]] matches a string against an Extended Regular Expression. Captured groups land in BASH_REMATCH.
input="2024-07-15"
if [[ "$input" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})$ ]]; then
year="${BASH_REMATCH[1]}"
month="${BASH_REMATCH[2]}"
day="${BASH_REMATCH[3]}"
echo "Year=$year Month=$month Day=$day"
else
echo "Invalid date format" >&2
exit 1
fiCommon Validation Patterns
# IPv4 address (simplified)
ip_pattern='^([0-9]{1,3}\.){3}[0-9]{1,3}$'
[[ "$ip" =~ $ip_pattern ]] || { echo "Bad IP"; exit 1; }
# Email (basic)
email_pattern='^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'
[[ "$email" =~ $email_pattern ]] || { echo "Bad email"; exit 1; }
# Semantic version
semver_pattern='^[0-9]+\.[0-9]+\.[0-9]+$'
[[ "$version" =~ $semver_pattern ]] || { echo "Bad version"; exit 1; }
# Alphanumeric slug
slug_pattern='^[a-z0-9-]+$'
[[ "$slug" =~ $slug_pattern ]] || { echo "Invalid slug"; exit 1; }Store the pattern in a variable before using it in [[ ]] — quoting a regex literal inside [[ ]] treats it as a literal string, not a regex. [[ $s =~ "$pattern" ]] won't match as regex; [[ $s =~ $pattern ]] will.
