SlideShare a Scribd company logo
1 of 12
Regular Expression
Agenda
概述
• 如果想要使用好shell就需要对正则表达式有一
  定的了解, linux系统上很多工具程序都使用到
  了它, 包括:
  1, 文本处理工具grep/awk/sed;
  2, 文本编辑器vi/emacs
  还有其他的一些工具或多或少都用到了一些正
  则表达式相关的概念, 比如ls, find命令等;
• 本章将会使用vim中的查找替换功能来演示正
  则表达式的使用. 正则表达式的概念是相通的,
  也许在不同的工具中具体实现会稍有差别.
什么是正则表达式?
• 正则表达式(regular expression, 以后简写为
  RE)是一种字符模式, 用于在目标字符串中进
  行模式匹配. RE非常适合于文本的查找和替
  换工作, 最初起源于perl语言, 后来得到了广
  泛的使用, 目前几乎所有的编程语言中都提
  供了正则表达式功能;
• 沿用perl语言的书写习惯, RE通常书写为如
  下的格式: /expr/, 使用反斜杠/进行界定. 示
  例: /hello/, 这是一个最简单的正则表达式,
  匹配字符串hello.
元字符
• 如果正则表达式只能够使用字符本身进行匹配的话,
  那么它的作用不大; 正则表达式的真正威力来自于
  元字符(meta char)的使用, 元字符表达的是不同于字
  面本身的含义, 比如元字符可以用来表示:
  1, 位置, 比如行首/行尾/单词开始或结束;
  2, 字符类型, 比如数字字符/字母等;
  3, 字符范围, 比如a-z, 1-9等;
  4, 出现次数, 比如1次或多次, 0次或1次, 指定次数;
• 对于不同的正则表达式实现(vi, awk, grep等), 元字符
  的使用会有不同, 但基本都是相通的.
元字符
                       - vim
• vim支持的元字符列举如下:
  1, c 不区分大小写, 示例:
        /cTEST     # 不区分大小写查找test
  2, * 表示0个或者多个, 示例:
        /abc*       # 匹配abc, abcc......
  3, +表示0个或者多个, 示例:
         /abc+     # 匹配abcc, abccc......
  4, [] 匹配方括号内的任意一个字符, 示例:
        /[Tt]est    # 匹配test和Test
     [x-y]匹配x到y范围中的字符, 示例:
        [a-z]xy     # 匹配axy, bxy, cxy...
     [^] 匹配不在方括号中的字符, 示例:
        [^aA]xy     # 匹配bxy, Bxy...
  5, | 匹配两个模式中的任意一个, 示例:
        /Test|test # 匹配test和Test
元字符
                   - vim
6, = 匹配0个或者1个
       /ad=              # 匹配字符a后面跟0个或1个数字
7, ^表示行首, $表示行尾, 使用^, $分别表示^和$字符
8, s表示空白字符, 示例:
       /^s*$              # 匹配空行
9, d 表示数字字符, D表示非数字字符, 示例:
       /d+               #匹配由多个数组组成的文本
10, w 匹配一个单词
11, .匹配单个任意字符
其他元字符, 这些元字符可能是vim特有的:
1, {m}, {m,}, {m, n} 分别匹配模式出现的次数m次, 大于m次,
m-n次, 示例:
       /abc{1,3}         # 匹配abc, abcc, abccc
2, <, >分别表示词首, 词尾
3, (, )对模式进行分组, 该分组可以分别使用1, 2进行引用.示例:
        /([a-c]{2}){3} # 匹配模式[a-c]{2}出现3次
示例
                    - 基本正则表达式
• 首先打开vim编辑器, 输入下面的一段文本:
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by 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.
• 作如下测试:
/love               查找love
/^love              查找行首的love
/love$              查找行尾的love
/l.ve               查找l后面跟任意字符再跟ve字符
/o*ve               零个或多个o后面跟ve
示例
             - 基本正则表达式
/o+ve        一个或多个o后面跟ve
/o=ve        零个或一个o后面跟ve
/o{1,2}ve    1-2个o后面跟ve
/[Ll]ove      查找Love和love
/ove[a-z]     ove后面跟a-z中的任意字符
/ove[^a-z]    ove后面跟不在a-z范围内的字符
/<love>     查找单词love
/ove|oove    查找ove或者oove
示例
                           -元字符组合
• 打开vim编辑器输入如下内容:
Christian Scott lives here and will put on a Christmas party.
There are around 30 to 35 people invited.
They are:
                                Tom
Dan
  Rhonda Savage
Nicky and Kimberly.
Steve, Suzanne, Ginger and Larry.
• 作如下测试, 并查看结果:
  /^[A-Z]..$
  /^[A-Z][a-z ]*3[0-5]
  /[a-z]*.
  /^ *[A-Z][a-z][a-z]$
  /^[A-Za-z]*[^,][A-Za-z]*$
示例
                          - 其他元字符
• 打开vim编辑器输入以下文本:
 Unusual occurences happened at the fair.
 Patty won fourth place in the 50 yard dash square and fair.
 Occurences like this are rare.
 The winning ticket is 55222.
 The ticket I got is 54333 and Dee got 55544.
 Guy fell down while running around the south bend in his last event.
• 作如下测试:
  /<fourth>        # 查找单词fourth
  /<f.*th>/ # 查找f开头跟0个或多个任意字符以th结尾的
  任意单词或者词组
  # 将Occurence或者occurence分别替换为Occurrence和
  occurrence
  :1,$s/([Oo]ccur)ence/1rence/
  # 将当前行的square and fair替换为fair and square
  :s/(square) and (fair)/2 and 1
END

More Related Content

More from ted-xu

11, OCP - awr & alert system
11, OCP - awr & alert system11, OCP - awr & alert system
11, OCP - awr & alert systemted-xu
 
10, OCP - flashback
10, OCP - flashback10, OCP - flashback
10, OCP - flashbackted-xu
 
9, OCP - restore and recovery with rman
9, OCP - restore and recovery with rman9, OCP - restore and recovery with rman
9, OCP - restore and recovery with rmanted-xu
 
8, OCP - backup with rman
8, OCP - backup with rman8, OCP - backup with rman
8, OCP - backup with rmanted-xu
 
7, OCP - configure database for backup and recovery
7, OCP - configure database for backup and recovery7, OCP - configure database for backup and recovery
7, OCP - configure database for backup and recoveryted-xu
 
6, OCP - oracle security
6, OCP - oracle security6, OCP - oracle security
6, OCP - oracle securityted-xu
 
5, OCP - oracle storage
5, OCP - oracle storage5, OCP - oracle storage
5, OCP - oracle storageted-xu
 
4, OCP - oracle networking
4, OCP - oracle networking4, OCP - oracle networking
4, OCP - oracle networkingted-xu
 
3, OCP - instance management
3, OCP - instance management3, OCP - instance management
3, OCP - instance managementted-xu
 
2, OCP - installing and creating a database
2, OCP - installing and creating a database2, OCP - installing and creating a database
2, OCP - installing and creating a databaseted-xu
 
1, OCP - architecture intro
1, OCP - architecture intro1, OCP - architecture intro
1, OCP - architecture introted-xu
 
12, OCP - performance tuning
12, OCP - performance tuning12, OCP - performance tuning
12, OCP - performance tuningted-xu
 
7, business event system
7, business event system7, business event system
7, business event systemted-xu
 
6, workflow miscellaneous
6, workflow miscellaneous6, workflow miscellaneous
6, workflow miscellaneousted-xu
 
5, workflow function activity
5, workflow function activity5, workflow function activity
5, workflow function activityted-xu
 
4, workflow tables & api
4, workflow tables & api4, workflow tables & api
4, workflow tables & apited-xu
 
3, workflow in ebs
3, workflow in ebs3, workflow in ebs
3, workflow in ebsted-xu
 
2, a simple workflow
2, a simple workflow2, a simple workflow
2, a simple workflowted-xu
 
1, workflow intro
1, workflow intro1, workflow intro
1, workflow introted-xu
 
8, bes tables & api
8, bes tables & api8, bes tables & api
8, bes tables & apited-xu
 

More from ted-xu (20)

11, OCP - awr & alert system
11, OCP - awr & alert system11, OCP - awr & alert system
11, OCP - awr & alert system
 
10, OCP - flashback
10, OCP - flashback10, OCP - flashback
10, OCP - flashback
 
9, OCP - restore and recovery with rman
9, OCP - restore and recovery with rman9, OCP - restore and recovery with rman
9, OCP - restore and recovery with rman
 
8, OCP - backup with rman
8, OCP - backup with rman8, OCP - backup with rman
8, OCP - backup with rman
 
7, OCP - configure database for backup and recovery
7, OCP - configure database for backup and recovery7, OCP - configure database for backup and recovery
7, OCP - configure database for backup and recovery
 
6, OCP - oracle security
6, OCP - oracle security6, OCP - oracle security
6, OCP - oracle security
 
5, OCP - oracle storage
5, OCP - oracle storage5, OCP - oracle storage
5, OCP - oracle storage
 
4, OCP - oracle networking
4, OCP - oracle networking4, OCP - oracle networking
4, OCP - oracle networking
 
3, OCP - instance management
3, OCP - instance management3, OCP - instance management
3, OCP - instance management
 
2, OCP - installing and creating a database
2, OCP - installing and creating a database2, OCP - installing and creating a database
2, OCP - installing and creating a database
 
1, OCP - architecture intro
1, OCP - architecture intro1, OCP - architecture intro
1, OCP - architecture intro
 
12, OCP - performance tuning
12, OCP - performance tuning12, OCP - performance tuning
12, OCP - performance tuning
 
7, business event system
7, business event system7, business event system
7, business event system
 
6, workflow miscellaneous
6, workflow miscellaneous6, workflow miscellaneous
6, workflow miscellaneous
 
5, workflow function activity
5, workflow function activity5, workflow function activity
5, workflow function activity
 
4, workflow tables & api
4, workflow tables & api4, workflow tables & api
4, workflow tables & api
 
3, workflow in ebs
3, workflow in ebs3, workflow in ebs
3, workflow in ebs
 
2, a simple workflow
2, a simple workflow2, a simple workflow
2, a simple workflow
 
1, workflow intro
1, workflow intro1, workflow intro
1, workflow intro
 
8, bes tables & api
8, bes tables & api8, bes tables & api
8, bes tables & api
 

3, regular expression

  • 3. 概述 • 如果想要使用好shell就需要对正则表达式有一 定的了解, linux系统上很多工具程序都使用到 了它, 包括: 1, 文本处理工具grep/awk/sed; 2, 文本编辑器vi/emacs 还有其他的一些工具或多或少都用到了一些正 则表达式相关的概念, 比如ls, find命令等; • 本章将会使用vim中的查找替换功能来演示正 则表达式的使用. 正则表达式的概念是相通的, 也许在不同的工具中具体实现会稍有差别.
  • 4. 什么是正则表达式? • 正则表达式(regular expression, 以后简写为 RE)是一种字符模式, 用于在目标字符串中进 行模式匹配. RE非常适合于文本的查找和替 换工作, 最初起源于perl语言, 后来得到了广 泛的使用, 目前几乎所有的编程语言中都提 供了正则表达式功能; • 沿用perl语言的书写习惯, RE通常书写为如 下的格式: /expr/, 使用反斜杠/进行界定. 示 例: /hello/, 这是一个最简单的正则表达式, 匹配字符串hello.
  • 5. 元字符 • 如果正则表达式只能够使用字符本身进行匹配的话, 那么它的作用不大; 正则表达式的真正威力来自于 元字符(meta char)的使用, 元字符表达的是不同于字 面本身的含义, 比如元字符可以用来表示: 1, 位置, 比如行首/行尾/单词开始或结束; 2, 字符类型, 比如数字字符/字母等; 3, 字符范围, 比如a-z, 1-9等; 4, 出现次数, 比如1次或多次, 0次或1次, 指定次数; • 对于不同的正则表达式实现(vi, awk, grep等), 元字符 的使用会有不同, 但基本都是相通的.
  • 6. 元字符 - vim • vim支持的元字符列举如下: 1, c 不区分大小写, 示例: /cTEST # 不区分大小写查找test 2, * 表示0个或者多个, 示例: /abc* # 匹配abc, abcc...... 3, +表示0个或者多个, 示例: /abc+ # 匹配abcc, abccc...... 4, [] 匹配方括号内的任意一个字符, 示例: /[Tt]est # 匹配test和Test [x-y]匹配x到y范围中的字符, 示例: [a-z]xy # 匹配axy, bxy, cxy... [^] 匹配不在方括号中的字符, 示例: [^aA]xy # 匹配bxy, Bxy... 5, | 匹配两个模式中的任意一个, 示例: /Test|test # 匹配test和Test
  • 7. 元字符 - vim 6, = 匹配0个或者1个 /ad= # 匹配字符a后面跟0个或1个数字 7, ^表示行首, $表示行尾, 使用^, $分别表示^和$字符 8, s表示空白字符, 示例: /^s*$ # 匹配空行 9, d 表示数字字符, D表示非数字字符, 示例: /d+ #匹配由多个数组组成的文本 10, w 匹配一个单词 11, .匹配单个任意字符 其他元字符, 这些元字符可能是vim特有的: 1, {m}, {m,}, {m, n} 分别匹配模式出现的次数m次, 大于m次, m-n次, 示例: /abc{1,3} # 匹配abc, abcc, abccc 2, <, >分别表示词首, 词尾 3, (, )对模式进行分组, 该分组可以分别使用1, 2进行引用.示例: /([a-c]{2}){3} # 匹配模式[a-c]{2}出现3次
  • 8. 示例 - 基本正则表达式 • 首先打开vim编辑器, 输入下面的一段文本: I had a lovely time on our little picnic. Lovers were all around us. It is springtime. Oh love, how much I adore you. Do you know the extent of my love? Oh, by 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. • 作如下测试: /love 查找love /^love 查找行首的love /love$ 查找行尾的love /l.ve 查找l后面跟任意字符再跟ve字符 /o*ve 零个或多个o后面跟ve
  • 9. 示例 - 基本正则表达式 /o+ve 一个或多个o后面跟ve /o=ve 零个或一个o后面跟ve /o{1,2}ve 1-2个o后面跟ve /[Ll]ove 查找Love和love /ove[a-z] ove后面跟a-z中的任意字符 /ove[^a-z] ove后面跟不在a-z范围内的字符 /<love> 查找单词love /ove|oove 查找ove或者oove
  • 10. 示例 -元字符组合 • 打开vim编辑器输入如下内容: Christian Scott lives here and will put on a Christmas party. There are around 30 to 35 people invited. They are: Tom Dan Rhonda Savage Nicky and Kimberly. Steve, Suzanne, Ginger and Larry. • 作如下测试, 并查看结果: /^[A-Z]..$ /^[A-Z][a-z ]*3[0-5] /[a-z]*. /^ *[A-Z][a-z][a-z]$ /^[A-Za-z]*[^,][A-Za-z]*$
  • 11. 示例 - 其他元字符 • 打开vim编辑器输入以下文本: Unusual occurences happened at the fair. Patty won fourth place in the 50 yard dash square and fair. Occurences like this are rare. The winning ticket is 55222. The ticket I got is 54333 and Dee got 55544. Guy fell down while running around the south bend in his last event. • 作如下测试: /<fourth> # 查找单词fourth /<f.*th>/ # 查找f开头跟0个或多个任意字符以th结尾的 任意单词或者词组 # 将Occurence或者occurence分别替换为Occurrence和 occurrence :1,$s/([Oo]ccur)ence/1rence/ # 将当前行的square and fair替换为fair and square :s/(square) and (fair)/2 and 1
  • 12. END