正则表达式

正则元字符

^    行首定位符                                       /^yoho/             匹配所有以yoho开头的行

$    行尾定位符                                      /yoho^/              匹配所有以yoho结尾的行

.     单个任意字符                                   /y..o/                  匹配以y开头后跟两个字符,再跟o的字符串

*    重复0到多个*前面的字符                   /a*/                    匹配0个或者多个a的字符串

[]   匹配一组字符的任意一个                   /[yk]oho/              匹配yoho 或者 koho的字符串

[x-y] 匹配指定范围内的一个字符             /[A-Z]oho/           匹配大写字符开头,后跟oho的字符串

[^]  匹配不在指定组内的字符                /[^A-Z]/               匹配不是大写字母的任意字符串

\   转义字符,用于转义元字符  

 

附加的元字符

\<       词首定位符                            /\<yoho/                     匹配以yoho开头的词

\>       词尾定位符                           /yoho\>/                      匹配以yoho结尾的词

\(..\)   给匹配的字符打上标签            /\(lov\able\1er/             模式lov被保存为标签1,用\1表示,查找一个字符串lov,后跟able,再跟lov,再接上er,即lovablelover

x\{m\}                                                                           字符x重复出现m次

x\{m,\}                                                                          字符x至少出现m次

x\{m,n\}                                                                        字符x至少出现m次,至多出现n次

 

POSIX类字符集

[[:alnum:]]                                                                    字母和数字

[[:alpha:]]                                                                     字母

[[:cntrl:]]                                                                       控制字符

[[:digit:]]                                                                       数字字符

[[:graph:]]                                                                     非空格(不包含空格,控制字符)

[[:lower:]]                                                                     小写字母

[[:print:]]                                                                      与[[:graph:]]类似,但包含空格

[[:punct:]]                                                                     标点符号

[[:space:]]                                                                    所有空白字符(空格、换行、制表符)

[[:upper:]]                                                                    大写字母

  [[:xdigit:]]                                                                  十六进制数字字符(o-9a-fA-F)

 

  

posted @ 2014-04-03 09:07  指间沙···  阅读(352)  评论(0编辑  收藏  举报