看了一点Ellie Quigley的《UNIX Shell 范例精解》,学了一点正则表达式入门的知识。总结一下:
 
正则表达式是一种字符模式,用于在查找过程中匹配相同的字符。在大多数程序里,正则表达式都被扩在2个正斜杠“/”之间。
 
(1)正则表达式元字符
---------------------------------------------------------------------------------------------------------
元字符                      功能                                     示例                               解释
--------------------------------------------------------------------------------------------------------
   ^                     行首定位符                           /^love/                        匹配以love开头的行
   $                     行尾定位符                           /love$/                        匹配以love结尾的行
                        匹配一个字符                       /l..e/                            匹配包含l,后跟2个任意字符,再跟一个e的行
   *                     匹配0或任意个前一字符  /o*ve/                         匹配包含任意个o后跟ve的行
  [ ]                    匹配集合中任意一个         /[Ll]ove/                      匹配Love或者love
[x-y]                  匹配范围中任意一个         /[a-z]/                           匹配任意一个小写字符
 [^]                     匹配不在指定组内字符    /[^a-z]/                         匹配非小写字母的一个字符
   \                      用于转义字符                     /love\./                         匹配包含love后跟一个句号的行
(以下是多数使用正则表达式元字符的UNIX程序都支持的元字符):
 \<                      词首定位符                         /\<love/                       匹配以love开头的词的行(vi and grep)
 \>                      词尾定位符                         /love\>/                       匹配以love结尾的词的行
(vi and grep)
\(..\)                   匹配标签                            /\(love\)\1er/                love模式被保存为标签1,此后\1就等价于love
x\{m\}               字符x的m次重复出现       a\{3\}  
x\{m,\}              字符x至少出现m次          a\{3,\}
x\{m,n\}            字符x至少出现m次,至多n次 a\{2,8\}  
 
(2)组合正则表达式元字符
元字符可以组合成为更为复杂一些的表达式,例如:
2.1 /^[A-Z]..$/
查找的行以大写字母开头,后跟2个任意字符,后跟换行符
2.2/^[A-Z][a-z]*3[0-8]/
查找的行以大写字母开头,后跟0或任意个小写字母,后跟数字3,后跟一个0-8之间的一个数字
2.3/[a-z]*\./
查找的行包含0个或者多个小写字母后跟一个句点
2.4/*[A-Z][a-z][a-z]$/
查找的行以若干空格开头,后跟一个大写字母2个小写字母,后跟换行
2.5/^[A-Za-z]*[^,][A-Za-z]*$/
查找的行以0或多个大小写字母开头,后面不是句号,后跟0或多个大小写字母,然后换行
 
(3)使用举例
可以找到多个匹配项时,用n
3.1 关于^和[]
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy the way,I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love
is forever. I live for you. It's hard to get back in
the groove.
~
~
~
/^[Ll]ove/
说明:匹配以love或者Love开头
 
3.2 关于.
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy the way,I think
I lost my gloves somewhere out in that field of
clover. Did you
see them? I can only hope love
is forever. I live for you. It's hard to get back in
the groove.
~
~
~
/l.ve/
说明:匹配包含l后跟任意字符+ve的行
 
3.3 关于*
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy the way,I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love
is forever. I live for you. It's hard to get back in
the groove.
~
~
~
/o*ve/
说明:*是与前面字符紧密联系的,如果是/*a/则是表示匹配包含任意个空格之后含a的行
 
3.4 关于[^]
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy the way,I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love
is forever. I live for you. It's hard to get back in
the groove.
~
~
~
/ove[^a-zA-Z0-9]/
说明:空格可以被匹配到,但是换行符不能被匹配到。这里是找ove后面跟一个非大消息字母及数字的行。
 
3.5 关于$和[]
I had a lovely time on our little picnic.
Lovers were all around us.It is springtime.Oh
love,how much I adore you.Oh,buy the way,I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love
is forever. I live for you. It's hard to get back in
the groove.
~
~
~
/lov[a-z]$/