Introduction
Regex helps find specific patterns in text easily. It works like a smart search tool that can match letters, numbers, or symbols in many ways.
grep 'pattern' filename # or [[ string =~ regex_pattern ]]
grep '^Hello' file.txtgrep '[0-9]\{3\}' file.txt[[ 'abc123' =~ [a-z]+[0-9]+ ]] && echo 'Match!'
[[ '' =~ .* ]] && echo 'Empty string matches any pattern'
#!/bin/bash input_string="user@example.com" if [[ $input_string =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then echo "Valid email address" else echo "Invalid email address" fi
text="hello123" if [[ $text =~ ^hello[0-9]+$ ]]; then echo "Matched" else echo "No match" fi
if [ $input =~ ^test ]; then echo "Yes"; fi
Pass the test and this pattern moves unknown