Regular Expression: Basic Regular Expression
Posix Regular Expression has three sets of compliance:BRE Basic Regular Expression, ERE: Extended Regular Expression and SRE: Simple Regular Expression. SRE is deprecated, in favour of BRE.
In Posix Extended Regular Expression: There are 14 Metacharacter
Basic Regular Expression DOES need to be escaped: [Preceded by a backslash \] Metacharacter
Extended Regular Expression DOES NOT need to be escaped Metacharacter
Posix Regular Expression 14 metacharacters
[ ] |
Character Class |
( ) |
Group |
{ } |
Range |
^ |
Begin of String |
$ |
End of String |
? |
Zero or One |
. |
Any non-newline character |
| |
Logic OR |
+ |
One or more |
- |
From To |
POSIX Basic Regular Expression, Escape metacharacter ( ) and { }
POSIX Basic Regular Expression |
POSIX Extended Regular Expression |
\ ( \ ) |
( ) |
\{ \} |
{ } |
|
[ ] |
\| \+ \? |
^ $ ? . * | + - |
Regular Expression: Logic OR
grep 'dog\|cat' f.txt # POSIX Basic Regular Expression BRE
↑
+ → BRE
grep -E 'dog|cat' f.txt # POSIX Extended Regular Regular EBE
↑
+ → ERE
Regular Expression: Range
Regular Expression: 3 to 5 digits
grep 'try[0-9]\{3,5\}.tex' # POSIX Basic Regular Expression BRE
↑ ↑
+ - - + → BRE
grep -E 'try[0-9]{3,5}.tex' # POSIX Extended Regular Expression ERE
↑ ↑
+ - + → ERE
Regular Expression: zero or one
grep 'try[0-9]\?.tex' # POSIX Basic Regular Expression BRE
↑
+ → BRE
grep -E 'try[0-9]?.tex' # POSIX Extended Regular Expression ERE
↑
+ → ERE
POSIX Character Class
Character Class
POSIX class Similar to Meaning
[:upper:] [A-Z] uppercase letters
[:lower:] [a-z] lowercase letters
[:alpha:] [[:upper:][:lower:]] upper- and lowercase letters
[:alnum:] [[:alpha:][:digit:]] digits, upper- and lowercase letters
[:digit:] [0-9] digits
[:xdigit:] [0-9A-Fa-f] hexadecimal digits
[:blank:] [ \t] space and TAB characters only
[:space:] [ \t\n\r\f\v] blank (whitespace) characters
[:cntrl:] control characters
[:graph:] [^ \t\n\r\f\v] printed characters
[:print:] [^\t\n\r\f\v] printed characters and space