Metacharacter | Function | Example | What It Matches |
---|---|---|---|
^ | ^love | Matches all lines beginning with love. | |
$ | love$ | Matches all lines ending with love. | |
. | l..e | Matches lines containing an l, followed by two characters, followed by an e. | |
* | *love | Matches lines with zero or more spaces, of the preceding characters followed by the pattern love. | |
[ ] | [Ll]ove | Matches lines containing love or Love. | |
[^] | Matches one character not in the set | [^A–K]ove | Matches lines not containing A through K followed by ove. |
\<[a] | Beginning-of-word anchor | \<love | Matches lines containing a word that begins with love. |
\> | End-of-word anchor | love\> | Matches lines containing a word that ends with love. |
\(..\)[b] | Tags matched characters | \(love\)able | Tags marked portion in a register to be remembered later as number 1. To reference later, use \1 to repeat the pattern. May use up to nine tags, starting with the first tag at the leftmost part of the pattern. For example, the pattern love is saved in register 1 to be referenced later as \1. |
x\{m\} x\{m,\} x\{m,n\}[c] | Repetition of character x: m times, at least m times, or between m and n times | o\{5\} o\{5,\} o\{5,10\} | Matches if line has 5 occurrences of o, at least 5 occurrences of o, or between 5 and 10 occurrences of o. |
Alphanumeric word character; [a-zA-Z0-9_] | l\w*e | Matches an l followed by zero more word characters, and an e. | |
\W | Nonalphanumeric word character; [^a-zA-Z0-9_] | love\W+ | Matches love followed by one or more nonword characters (., ?, etc.). |
\b | Word boundary | \blove\b | Matches only the word love. |
The Basic Set—GNU grep's Regular Expression Metacharacters
最新推荐文章于 2018-11-28 11:17:00 发布