Regex
Appearance
General
- xxx
Characters
- ^
- beginning of the line
- $
- end of line
- .
- any single character
- [ ]
- range of characters
- to use any of these special characters as normal characters, preface them with a \.
- \$
Special match patterns
- [:alnum:] => alphanumeric character
- [:alpha:] => letters
- [:asccii:] => ascii characters
- [:blank:] => space or tab
- [:cntrl:] => ascii control characters
- [:digit:] => digits
- [:graph:] => noncontrol, nonspace characters
- [:lower:] => lowercase
- [:print:] => printable characters
- [:punct:] => punctuation characters
- [:space:] => whitespace characters, including vertical tab
- [:upper:] => uppercase
- [:xdigit:] => hex digits
Extended regex characters
- ?
- match is optional, but may be matched once at most
- *
- must be matched zero or more times
- +
- must be matched one or more times
- {n}
- must be matched n times
- {n,}
- must be matched n or more times
- {n,m}
- must be matched between n or m times inclusive
Examples
- grep e$ blah.txt
- find words that end in e in the file blah.txt
- grep ablank: blah.txt
- find words that end with the letter a. blank checks for a space or tab after a
- grep Th.space: blah.txt
- find three letter words that start with Th
- grep -E [a-z]\{10\} blah.txt
- Extended grep that searches for lowercase words that are exactly 10 characters long
- I guess the curlies need to be escaped