Shell Common Regular Expressions
Common Regular expression ^ # line Header locates $ # end of row. # match any character except line break * # match 0 or more repeating characters + # Repeat once or more? # repeat 0 times or once? # end the greedy factor. *? Represents a minimum match [] # matches any one of the characters in a set [^] # Matches characters not in the specified group \ # used to escape metacharacters < # Word-head Locators (Support VI and grep) <love> # Final Locator (Support VI and grep) love>x\{m\} # repeated occurrences m times x\{m,\} # repeated occurrences at least m times x\{m,n\} # repeats at least m times not more than n x? # match occurs 0 times or once in uppercase letters xx+ # matches one or more letters X () # The characters within the parentheses for a set (Ab|de) + # match a series of (at least one) ABC or DEF;ABC and Def will match [[: alpha:]]< c23/># stands for all letters regardless of the case [[: Lower:] # = lowercase letters [[: Upper:]] # means uppercase [[:d Igit:]] # denotes numeric characters [[:d Igit:][:lower:]]
Metacharacters
\d # matches any one digit \d # matches any single non-numeric character \w # matches any single alphanumeric underscore character, synonym is [: alnum:]\w # matches non-numeric characters
Character classes: whitespace characters
\s # matches any whitespace character \s # matches non-whitespace characters \b # matches the beginning or end of a word \ n # matches a newline character \ r # matches a carriage return \ t # match tab \b # match backspace # match a null value character
Character classes: Anchoring characters
\b # Match word boundary (not in []) \b # matches non-word boundary \a # Match string beginning \z # Match string or end of line \z # Match string only end \g # Match Previous m// g Where to leave
Capture
(exp) # match Exp, and capture text into the automatically named group (? <name>exp) # Match exp, and capture the text to a group named name, you can also write (? ' Name ' Exp ' (?: exp) # matches exp, does not capture matched text, and does not assign group numbers to this group
0 Wide Assertion
(? =exp) # match exp front position (? <=exp) # Match exp back to position (?! EXP) # match the position followed by the exp (? <!exp) # match the location (? #comment) that is not in front of exp (?) # Comments do not have any effect on the processing of regular expressions for commenting
Special characters
Http://en.wikipedia.org/wiki/ascii_table^h \010 \b ^m \015 \ R matches special characters: Ctrl + V CTRL does not press H or M to output ^h for matching
Shell Regular Expression