-
-
Save kbbgl/c6c4457d3d61ea6715b905e88fd029b4 to your computer and use it in GitHub Desktop.
[match character classes] #regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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