Match Character Classes
Vowels
echo abcdefghijk | grep -e "[aeiou]"
# will find a,e,i
Range
echo abcdefghijkAC04 | grep -e "[a-zA-D0-9]"
Negation character ^ - don't match
echo abcdefghijkAC04 | grep -e "[^a-zA-D0-9]"
# AC04
select with or (alternation)
echo "cat dog" | grep -e "[cat|fish]" # selects cat
Using metacharacters
select only characters (including _)
echo "P45SW0%$#_#2#4RD" | grep -e "\w" # selects P45SW0RD
select only special characters
echo "P45SW0%$#_#2#4RD" | grep -e "\W" # selects %$#_##
select all digits (-P flag enables perl regex)
echo "P45SW0%$#_#2#4RD" | grep -P "\d"
# matches
select all except digits (-P flag enables perl regex)
echo "P45SW0%$#_#2#4RD" | grep -P "\D" # matches
select tabs, newlines
echo "some text with tab" | grep -P "\t"
# matches tab
echo "some text with tab" | grep -P "\n"
# matches newline
echo "some text with tab" | grep -P "\s"
# matches spaces
echo "some text with tab" | grep -P "\S"
# matches sometextwiththab
# match square brackets
echo "[]" | grep -e "\[\]"