Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1287749
  • 博文数量: 632
  • 博客积分: 2778
  • 博客等级: 大尉
  • 技术积分: 3387
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-31 09:09
个人简介

123

文章分类

全部博文(632)

文章存档

2014年(36)

2013年(33)

2012年(563)

分类: LINUX

2012-10-25 16:57:34

正则表达式 学习笔记

转自:http://blog.csdn.net/yaozhiyi/article/details/6104487

1 . regular expressions 分类
--BRE(Basic Regular Expressions 基础正则表达式)
--ERE(Extended Regular Expressions 扩展正则表达式)

2 . BRE支持的元符号
1)  .   匹配且仅匹配任意一个字符; .*表示长度为0或以上的任意字符串
2)  *   与*之前的字符合作,表示该字符重复0次或者0次以上
3)  ^   用于锚定字符串的开始 , '^head' 表示匹配以head开头的行
4)  $   用于锚定字符串的结束 , 'tail$' 表示匹配以tail结尾的行
5)  /   转义字符,消除字符的特殊含义 , '/''表示匹配单引号'
6)  []  匹配其中出现的字符 , '[adf]'表示必须匹配a或d或f中的一个,'[a-zA-Z1-9]'表示匹配所有字母和数字中的一个
7)  [^] 匹配不在其中出现的字符 , [^1-9] 表示必须匹配不属于1-9的字符中的一个
8)  /{/}放在字符后表示该字符重复的次数 , a/{3/}表示匹配3个a, a/{3,/}表示匹配至少3个a, a/{3,6/}表示匹配3-6个a
9)  /(/)标记匹配的字符,grep '/(ab.c/)de/(bb*/)middle/1/2' 其中用/将1转义,即/1表示匹配与正则式ab.c已经匹配的串,/2则表示匹配与bb*已经匹配的串
10) /锚定单词的开始与结束 , grep '/'匹配以gin结尾的单词,'/'则只能匹配单词son

3 . ERE支持的元符号
1)  ?   与?之前的字符合作,表示该字符或者出现或者不出现
2)  |   用于匹配正则表达式集合中的一个,类似于或
3)  +   与+之前的字符配合,表示该字符连续出现至少一次
4)  {}  与BRE中的/{/}功能相同; 即a{3}匹配3个a,a{3,}匹配至少3个a , a{3,6}匹配3-6个a
5)  ()  指明某正则式作为一个整体,可以与|配合

4 . BRE 与 ERE 的异同
1)  . * ^ $ / [] [^] 具有相同的功能
2)  ERE 不支持/(/) 以及 /{/}
3)  ERE 比 BRE 多支持 + ? | () 以及 {}
4)  ERE 的{} 与 /{/} 具有相同的功能

5 . 方括号表达式
1)  方括号表达式(bracket expression)是由[ 和 ] 括起来的字符列表,他能匹配字符列表中的任一个字符
2) 如果字符列表以 ^ 开头, 则它能够匹配不在字符列表中的字符

6 . 方括号表达式中的元字符
1) 在方括号表达式中大多数元字符丧失了他们的特殊含义 , 包括元字符/ . * $ ( ) + < > 
2) 如果想在方括号表达式中匹配一个字面意义的] , 那么应该把] 加到方括号表达式中的第一位
3) 如果想在方括号表达式中匹配一个字面意义的^ ,那么绝不该把 ^ 加到方括号表达式中的第一位
4) 如果想在方括号表达式中匹配一个字面意义的- , 那么应该把- 加到方括号表达式中的最后一位
5) 以上出自man grep 并有我自己的补充 Most meta-characters lose their special meaning inside bracket expressions.  To include a literal ] place it first in the list. Similarly , to include a literal ^ place it anywhere but first. Finally, to include a literal - place it last.

7 . 方括号表达式中的引号('和")
举grep 为例
1) grep ['] file 非法
2) grep ["] file 非法
3) grep "[']" file 合法
4) grep '["]' file 合法
5) grep "["]" file 非法

6) grep '[']' file 非法


8 . 方括号表达式(bracket expression) 中预定义(predefined)的字符类(class of characters)
1)  [:alnum:]  即[a-zA-Z1-9]     (alphabet and number)
2)  [:alpha:]  即[a-zA-Z]        (alphabet)
3)  [:cntrl:]                   (control)
4)  [:lower:]  即[a-z]           (lower case)
5)  [:upper:]  即[A-Z]           (upper case)
6)  [:ascii:]
7)  [:digit:]
8)  [:blank:]  空格或者tab
9)  [:print:]  可打印字符
10)[:punct:]  标点符号
11)[:space:]  空格或tab或者垂直tab
12) [:xdigit:] 十六进制数字

9 . 方括号表达式实例验证
1)  准备文件file , 内容如下:
[aa]
{bb}
'cc'
"dd"
/ee/
(ff)
^gg^
$hh$
*ii*
.jj.
?kk?
|ll|
+mm+
-nn-

!vv!
`ww`

2)  验证]需要放在方括号表达式字符列表的最前
owner@YZY:~/notes$ grep '[!]]' file
owner@YZY:~/notes$ grep '[]!]' file
[aa]
!vv!
owner@YZY:~/notes$

3) 验证 ^ 绝不能放在方括号表达式字符列表的最后一位
owner@YZY:~/notes$ grep '[^]' file ===> 失败
grep: Unmatched [ or [^
owner@YZY:~/notes$ grep '[^!]' file ===> 匹配所有不等于!的字符
[aa]
{bb}
'cc'
"dd"
/ee/
(ff)
^gg^
$hh$
*ii*
.jj.
?kk?
|ll|
+mm+
-nn-

!vv!
`ww`
owner@YZY:~/notes$ grep '[!^]' file ===> ^ 放在最后,匹配字符 ! 或者 ^
^gg^
!vv!
owner@YZY:~/notes$ grep '[!^$]' file ===> 试试把 ^ 放在中间
^gg^
$hh$
!vv!
owner@YZY:~/notes$
 

4)  验证 - 必须放在方括号表达式的最后方
owner@YZY:~/notes$ grep '[-]' file
-nn- ==>匹配了两个 -
owner@YZY:~/notes$ grep '[a-]' file
[aa]  ==> 匹配了两个 a
-nn-  ==> 匹配了两个 -
owner@YZY:~/notes$ grep '[-a]' file ==>好像没错阿!那么,-不要放在两个字符中间就好了!
[aa]  ==> 匹配了两个 a
-nn-  ==> 匹配了两个 -
owner@YZY:~/notes$ grep '[ac-]' file
[aa]  ==> 匹配了两个 a
'cc'  ==> 匹配了两个 c
-nn-  ==> 匹配了两个 -
owner@YZY:~/notes$ grep '[a-c]' file
[aa]  ==> 匹配了两个 a
{bb}  ==> 匹配了两个 b
'cc'  ==> 匹配了两个 c
owner@YZY:~/notes$ grep '[c-a]' file  ===> c-a 是一个无效的范围,匹配不了
owner@YZY:~/notes$

5)  验证{}[]()+.*/|?$在[]失效(前提:保证对- ^ ] 的位置做出正确的安排 ,并且其中的引号要特殊处理)
--^-] 位置的安排参考 6  的论述
--引号的处理参考 7 的论述
10. 方括号表达式预定义字符类验证
1) 准备字符文件 char_class ,每个字符一行,倒数2为空格,倒1为tab键:
owner@YZY:~/notes$ cat -A char_class
c$
C$
5$
`$
~$
!$
@$
#$
$$
%$
^$
&$
*$
($
)$
-$
=$
+$
[$
]$
{$
}$
/$
|$
:$
;$
'$
"$
<$
>$
?$
/$
,$
.$
 $
^I$
 owner@YZY:~/notes$

2)[:alnum:] [:digit:] [:xdigit:] [:alpha:] [:lower:] [:upper:]
owner@YZY:~/notes$ cat char_class | grep '[[:alnum:]]'
c
C
5
owner@YZY:~/notes$ cat char_class | grep '[[:digit:]]'
5
owner@YZY:~/notes$ cat char_class | grep '[[:xdigit:]]'
c
C
5
owner@YZY:~/notes$ cat char_class | grep '[[:alpha:]]'
c
C
owner@YZY:~/notes$ cat char_class | grep '[[:lower:]]'
c
owner@YZY:~/notes$ cat char_class | grep '[[:upper:]]'
C
owner@YZY:~/notes$

3)[:graph:]
owner@YZY:~/notes$ cat char_class | grep '[^[:graph:]]'
 
   
owner@YZY:~/notes$
//输出一个是空格一个是tab键,等练好了awk,我给显示出来.

4)[:cntrl:]
这一部分没想法

5)[:print:]
wner@YZY:~/notes$ cat char_class | grep '[^[:print:]]'
   
owner@YZY:~/notes$
//tab不能输出
6) [:punct:]
owner@YZY:~/notes$ cat char_class | grep '[[:punct:]]'
`
~
!
@
#
$
%
^
&
*
(
)
-
=
+
[
]
{
}
/
|
:
;
'
"
<
>
?
/
,
.
owner@YZY:~/notes$

7) [:blank:]
owner@YZY:~/notes$ cat char_class | grep '[[:blank:]]'
 
   
owner@YZY:~/notes$
//空格和tab

8) [:ascii:]
非法的名字
owner@YZY:~/notes$ cat char_class | grep '[[:ascii:]]'
grep: Invalid character class name
owner@YZY:~/notes$

阅读(1163) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~