mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

       正則表達式非常实用, 有些书专门用整本书来讲这个, 可见其博大精深。 有人的地方就有江湖。 有字符串的地方就有正則表達式。所谓的正則表達式, 只是是一种模式/形式罢了。 说白了, 就是一个字符串形式。 没那么玄乎其玄。

       我们之前介绍过的grep, sed和awk是一种文本/字符串处理工具。 而正則表達式却不同。 它仅仅是一种字符串形式。 我们能够用grep, sed和awk对正則表達式进行处理。 为了方便集中介绍正則表達式, 我们用最简单的grep来做处理工具。

       正則表達式也不同于通配符。 虽然也有类似的地方。

在正則表達式中的*和通配符中的*就不是一个意思, 这一点务必引起重视。

       除了主要的正則表達式外, 实际上还有扩展的正則表達式。 比方+, ?, ()等东东。 此时我们要用egrep或者grep -E, 在本文中, 我们用egrep.


       以实践操作为荣, 以只看不练为耻。

在本文中,我们只进行基本介绍, 兴许有新东西加入到本博客中, 我们一起来玩玩吧:


       1. ^xxx表示以xxx開始的行, 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ cat test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep ^go test.txt
good good study

Administrator@51B6904C3C8A485 ~/reg
$


       2. xxx$表示以xxx结尾的行。 例如以下:
Administrator@51B6904C3C8A485 ~/reg
$ cat test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep up$ test.txt
daydayup

Administrator@51B6904C3C8A485 ~/reg
$

       顺便说一下, ^$能够表示空行, 这个显而易见。


       3. 点表示除了换行符之外的随意一个字符, 例如以下:
Administrator@51B6904C3C8A485 ~/reg
$ cat test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep d.y test.txt
daydayup

Administrator@51B6904C3C8A485 ~/reg
$

       再来看一个错误的使用方法:

Administrator@51B6904C3C8A485 ~/reg
$ echo "w.x.y.z" | grep "w.x"
w.x.y.z

Administrator@51B6904C3C8A485 ~/reg
$
       结果尽管侥幸正确。 但这不过碰巧而已。 不信。 请看:

Administrator@51B6904C3C8A485 ~/reg
$ echo "w_x_y_z" | grep "w.x"
w_x_y_z

Administrator@51B6904C3C8A485 ~/reg
$
       为什么过滤w.x的时候, 却把w_x_y_z过滤出来了呢? 原来, 在正則表達式中。 点不再是普通的点了。 点表示的是换行符之外的随意一个字符。

但倘若我们就是要过滤w.x这个串, 怎么办呢? 那就必需要用到\来转义了, 我们即将会介绍。 先来热热身:

Administrator@51B6904C3C8A485 ~/reg
$ echo "www.x.y.z" | grep "w\.x"
www.x.y.z

Administrator@51B6904C3C8A485 ~/reg
$ echo "w_x_y_z" | grep "w\.x"

Administrator@51B6904C3C8A485 ~/reg
$
      这就对了。



      4. *表示前面模式的0次或者多次反复, 例如以下:
Administrator@51B6904C3C8A485 ~/reg
$ cat test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep d.*y test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep s.*t test.txt
good good study

Administrator@51B6904C3C8A485 ~/reg
$ grep s.t test.txt

Administrator@51B6904C3C8A485 ~/reg

       比方注意。 s.t并无法过滤出相应的行。 而s.*t却能够, 由于.*表示0个或者多个字符


       5. []用来指定一个字符所述的集合, 要注意,[]仅仅会匹配当中的某个字符, 例如以下:
Administrator@51B6904C3C8A485 ~/reg
$ cat test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep s[rst]u test.txt
good good study

Administrator@51B6904C3C8A485 ~/reg
$ grep s[abc]u test.txt

Administrator@51B6904C3C8A485 ~/reg
$
       假设要表示全部的英文字母。 那该用如何的集合呢? 例如以下:
Administrator@51B6904C3C8A485 ~/reg
$ cat test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep s[a-zA-Z]u test.txt
good good study

Administrator@51B6904C3C8A485 ~/reg
$

       假设是数字, 那就用[0-9]限定即可了。 非常easy, 一笔带过。

       我们继续继续看例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ cat test1.txt
good good study
day day up
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep [a-z]ay test1.txt
day day up
daydayup

Administrator@51B6904C3C8A485 ~/reg
$
      能够看到, [a-z]ay把daydayup这一行也过滤出来了, 倘若我们仅仅要过滤出含有day单词的行, 那该怎么办呢? 我们以下就会讲。


      6. \ 表示转义。 比方\<xxx就是以xxx开头的单词, xxx\>表示以xxx结尾的单词。 例如以下(正則表達式最好都加上双引號吧):

Administrator@51B6904C3C8A485 ~/reg
$ cat test1.txt
good good study
day day up
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep "\<[a-z]ay\>" test1.txt
day day up

Administrator@51B6904C3C8A485 ~/reg
$
       对, 别忘了。 我们的-w选项也能够过滤单词行, 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ cat test1.txt
good good study
day day up
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep -w [a-z]ay test1.txt
day day up

Administrator@51B6904C3C8A485 ~/reg
$
        可是, 以下的结果可能会出乎你我的意料:

Administrator@51B6904C3C8A485 ~/reg
$ cat test2.txt
good good study
day day up
oh day'abc oh
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep "\<[a-z]ay\>" test2.txt
day day up
oh day'abc oh

Administrator@51B6904C3C8A485 ~/reg
$ grep -w [a-z]ay test2.txt
day day up
oh day'abc oh

Administrator@51B6904C3C8A485 ~/reg
$
       为什么day'abc所在的行也被过滤出来了呢? 这就涉及到正則表達式对单词的定义了。 '和空格符一样, 都是切割符号。


       7. []中的^表示反义

       我们知道, ^xxx表示以xxx开头的行, 可是。 在[]中的^, 就表示取反了, 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ cat test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep "d[^abcd]" test.txt
good good study

Administrator@51B6904C3C8A485 ~/reg
$ grep "d[^abcdy]" test.txt
good good study

Administrator@51B6904C3C8A485 ~/reg
$ grep "d[^abcdy ]" test.txt

Administrator@51B6904C3C8A485 ~/reg
$

      8. 一些字符类

      比方[[:lower:]] 等价于[a-z]

      比方[[:upper:]] 等价于[A-Z]

      其余的还有不少, 我们不一一列举。 来看一个上述[[:lower:]]的应用, 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ grep "^[[:lower:]]" test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep "^[[:upper:]]" test.txt

Administrator@51B6904C3C8A485 ~/reg
$

       9. 不得不说的反复

       前面我们已经说过, *表示对前面的字符反复0次或者多次。 我们再来复习一下:

Administrator@51B6904C3C8A485 ~/reg
$ cat test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep "d.*u" test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$ grep "d.*p" test.txt
daydayup

Administrator@51B6904C3C8A485 ~/reg
$
      假设要反复一次或者多次。 那就用+, 例如以下(注意, 例如以下要用扩展的正則表達式, 用grep -E或者直接用egrep):例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ echo "gd" | egrep "go*d"
gd

Administrator@51B6904C3C8A485 ~/reg
$ echo "gd" | egrep "go+d"

Administrator@51B6904C3C8A485 ~/reg
$ echo "god" | egrep "go+d"
god

Administrator@51B6904C3C8A485 ~/reg
$ echo "good" | egrep "go+d"
good

Administrator@51B6904C3C8A485 ~/reg
$
       那要表示只反复0次或者1次, 该怎么搞呢? 用问号即可了。 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ echo "good" | grep "go+d"

Administrator@51B6904C3C8A485 ~/reg
$ echo "gd" | egrep "go?

d" gd Administrator@51B6904C3C8A485 ~/reg $ echo "god" | egrep "go?d" god Administrator@51B6904C3C8A485 ~/reg $ echo "good" | egrep "go?d" Administrator@51B6904C3C8A485 ~/reg $

       那要是指定反复4次, 该怎么搞呢? 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ echo "goood" | egrep "go{4}d"

Administrator@51B6904C3C8A485 ~/reg
$ echo "gooood" | egrep "go{4}d"
gooood

Administrator@51B6904C3C8A485 ~/reg
$ echo "goooood" | egrep "go{4}d"

Administrator@51B6904C3C8A485 ~/reg
$
      那要是反复4次或以上, 该怎么搞呢? 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ echo "goood" | egrep "go{4,}d"

Administrator@51B6904C3C8A485 ~/reg
$ echo "gooood" | egrep "go{4,}d"
gooood

Administrator@51B6904C3C8A485 ~/reg
$ echo "goooood" | egrep "go{4,}d"
goooood

Administrator@51B6904C3C8A485 ~/reg
$
       那要是反复4次到6次, 该怎么搞呢? 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ echo "goood" | egrep "go{4,6}d"

Administrator@51B6904C3C8A485 ~/reg
$ echo "gooood" | egrep "go{4,6}d"
gooood

Administrator@51B6904C3C8A485 ~/reg
$ echo "goooood" | egrep "go{4,6}d"
goooood

Administrator@51B6904C3C8A485 ~/reg
$ echo "gooooood" | egrep "go{4,6}d"
gooooood

Administrator@51B6904C3C8A485 ~/reg
$ echo "goooooood" | egrep "go{4,6}d"

Administrator@51B6904C3C8A485 ~/reg
$
      说道这里, 关于反复的问题是非常明显了。 以下来小结一下:

      x*表示0个或者多个x

      x+表示1个或者多个x

      x?

表示0和或者1个x

      x{4}比表示4个x

      x{4,}表示4个或4个以上的x

      x{4,6}表示有有4个或者5个或者6个x


      既然已经学了这么多, 那想想怎样匹配出一个5位的正整数呢? 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ echo "123456" | egrep "[1-9][0-9]{4}"
123456

Administrator@51B6904C3C8A485 ~/reg
$ echo "1234" | egrep "\<[1-9][0-9]{4}\>"

Administrator@51B6904C3C8A485 ~/reg
$ echo "12345" | egrep "\<[1-9][0-9]{4}\>"
12345

Administrator@51B6904C3C8A485 ~/reg
$ echo "123456" | egrep "\<[1-9][0-9]{4}\>"

Administrator@51B6904C3C8A485 ~/reg
$


       10. ()表示总体, 例如以下:

Administrator@51B6904C3C8A485 ~/reg
$ echo "abababc" | egrep "(ab){3,}"
abababc

Administrator@51B6904C3C8A485 ~/reg
$ echo "abababc" | egrep "(ab){4,}"

Administrator@51B6904C3C8A485 ~/reg
$
        另外还要注意, 有时候会用()表示空(不是空格哈), 如:

Administrator@51B6904C3C8A485 ~/reg
$ echo "ab" | egrep "a b"

Administrator@51B6904C3C8A485 ~/reg
$ echo "ab" | egrep "a()b"
ab

Administrator@51B6904C3C8A485 ~/reg
$ echo "ab" | egrep "a b"

Administrator@51B6904C3C8A485 ~/reg
$ echo "a b" | egrep "a()b"

Administrator@51B6904C3C8A485 ~/reg
$ echo "a b" | egrep "a b"
a b

Administrator@51B6904C3C8A485 ~/reg
$

       11. |表示或, 非常好理解。

Administrator@51B6904C3C8A485 ~/reg
$ egrep "^g|p$" test.txt
good good study
daydayup

Administrator@51B6904C3C8A485 ~/reg
$
       如上就表示以g开头或者以p结尾的行。

       


       OK, 先说这么多。 兴许有新东东。 再补充。








       


posted on 2017-06-04 10:57  mthoutai  阅读(337)  评论(0编辑  收藏  举报