1 Regular Expressions 正则表达式
(1) Pattern could be wildcard or regular expression; most of them are the same usage; but some are different.
(2) Wildcard can be used in sed, awk, grep, egrep etc.; regular expression can be used in sed, awk, grep -E, egrep etc.
(3) grep -E, egrep and awk use extend regular expressions, perl even more extended.
Option or argument | Function |
(x) | Regular expression for grouping. |
^ | (1) 对行首 e.g. ^the对以the开头行 ^d..x..x..x 对用户、用户组及其他用户组成员有可执行权限的目录 |
$ | 对行尾 e.g. ^USER$ 对只包含USER的行 [tty]$ 对表示以 t 或者 y 结尾的行 [^$] 对空行 |
[xy] [a-c] | Any single character in the set specified. e.g [Ss]igna[lL]对匹配单词signal、signaL、Signal、SignaL [Ss]igna[lL]\. 对同上,但加一句点 [mayMAY] 对包含mayMAY中任一字母的行 [iI] [nN] 对大写或小写i或n [Dd]evice 对单词device或Device |
[^xy] [^a-c] | Any single character NOT in the set specified. e.g. [^0-9\$] 对非数字或美元标识 |
. | Any single character. e.g. ^......$ 对包括6个字符的行 |
x* [a-c]* | Zero or more occurrences of character x. e.g. [a-z][a-z]* 对至少一个小写字母 |
x+ expr+ | One or more occurrences of character x or regular expression. |
x? expr? | Zero or one occurrences of character x or regular expression. |
x|y | Either x or y. |
\x | (1) Force to read as letter, not as meaning it has. e.g. \^q对以^q开始行 ^\.[0-9][0-9] 对以一个句点和两个数字开始的行
(2) e.g. [0-9]\{2\}-[0-9]\{2\}-[0-9]\{4\} 对日期格式dd-mm-yyyy |
(order) | The operators order is [, ], *, ?, +, concatenation, | |
Symbol | ed | ex | vi | sed | awk | grep | egrep | Action |
. | Y | Y | Y | Y | Y | Y | Y | Match any character. |
* | Y | Y | Y | Y | Y | Y | Y | Match zero or more preceding. |
^ | Y | Y | Y | Y | Y | Y | Y | Match beginning of line. |
$ | Y | Y | Y | Y | Y | Y | Y | Match end of line. |
\ | Y | Y | Y | Y | Y | Y | Y | Escape character following. |
[ ] | Y | Y | Y | Y | Y | Y | Y | Match one from a set. |
\(\) | Y | Y |
| Y |
|
|
| Store pattern for later replay. |
\{\} | Y |
|
| Y |
| Y |
| Match a range of instances. |
\<\> | Y | Y | Y |
|
|
|
| Match word's beginning or end. |
+ |
|
|
|
| Y |
| Y | Match one or more preceding. |
? |
|
|
|
| Y |
| Y | Match zero or one preceding. |
| |
|
|
|
| Y |
| Y | Separate choices to match. |
() |
|
|
|
| Y |
| Y | Group expressions to match. |