Last active
July 21, 2019 06:03
-
-
Save estysdesu/eb5cc2260b319efa8bd2ad9793931d5c to your computer and use it in GitHub Desktop.
[Shell: regex (regular expression) patterns] #regex #sh #character #classes
This file contains hidden or 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
##### REGEX CHARACTERS/GLOBS ##### | |
. # match any one single character | |
* # match the previous character repeated 0+ times | |
? # match the previous character repeated 0 or 1 time (extended pattern `-E`) | |
+ # match the previous character repeated 1+ times (extended pattern `-E`) | |
{<num>} # match the previous character repeated the number of times specificed in the braces (extended pattern `-E`) | |
[<chars>] # character range to match one time; check `ascii` | |
[^<chars>] # character range to not match one time; check `ascii` | |
^ # if first char of pattern, pattern must be at start of line to match | |
$ # if last char of pattern, pattern must be at end of line to match | |
| # logical 'or' (extended pattern `-E`) | |
\ # escape regex characters | |
() # not regex; used to group characters | |
grep -E '^[0-9]+' <file> # only matches lines starting with one or digits | |
grep -E '(red){3}' <file> # only matches lines with 3 or more instances of 'red' | |
grep -E 'colou?r' <file> # matches 'color' or 'colour' | |
grep -E 'grey|gray' <file> # matches 'grey' or 'gray' | |
##### CHARACTER CLASSES (POSIX) ##### | |
# POSIX character classes generally require quoting or double brackets | |
[:alnum:] # matches alphabetic or numeric characters | |
[:alpha:] # matches alphabetic characters | |
[:blank:] # matches a space or a tab | |
[:digit:] # matches (decimal) digits | |
[:lower:] # matches lowercase alphabetic characters | |
[:space:] # matches whitespace characters (space and horizontal tab) | |
[:upper:] # matches uppercase alphabetic characters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment