Skip to content

Instantly share code, notes, and snippets.

@kbbgl
Created May 7, 2020 16:59
Show Gist options
  • Save kbbgl/c6c4457d3d61ea6715b905e88fd029b4 to your computer and use it in GitHub Desktop.
Save kbbgl/c6c4457d3d61ea6715b905e88fd029b4 to your computer and use it in GitHub Desktop.
[match character classes] #regex
# 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 "\[\]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment