码迷,mamicode.com
首页 > 系统相关 > 详细

Linux正则表达式

时间:2018-02-02 14:20:13      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:egrep   扩展   strong   ade   into   ext   root   符号   vim   

基础正则表达式(Basic Regular Expression)
序号 符号 描述 示例
1 ^ 匹配以……开头的行 ^ab匹配以ab开头的行
2 $ 匹配以……结尾的行 ab$匹配以ab结尾的行
3 ^$ 匹配空行 ^$匹配空行,不匹配空格
4 . 匹配任意单个字符 ab.匹配abc或abd,不匹配abcd或abde,包括空格
5 \ 转义符,将特殊符号进行转义 a\.b匹配a.b,不匹配ajb
6 * 匹配前面项0次或多次 ab*匹配a或ab或abbb
7 .* 匹配任意字符 ab.*匹配ab或abc或abcd,包括空行
8 [] 匹配集合以内的任意单个字符 ab[cd]匹配abc或abd,不匹配abj或abcd
9 [^] 匹配集合以外的任意单个字符 ab[^cd]匹配abe或abj,不匹配abc或abd
  • 数据模拟
[root@linux-node1 ~]# vim linbin.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billard ball and chinese chess.

my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org

my qq num is 49000488.

not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
  • 匹配以……开头的行
[root@linux-node1 ~]# grep "^m" linbin.txt
my blog is http://oldboy.blog.51cto.com
my qq num is 49000488.
my god,i am not oldbey,but OLDBOY
  • 匹配以……结尾的行
[root@linux-node1 ~]# grep "m$" linbin.txt
my blog is http://oldboy.blog.51cto.com
  • 匹配空行
[root@linux-node1 ~]# grep -vn "^$" linbin.txt
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball,billard ball and chinese chess.
6:my blog is http://oldboy.blog.51cto.com
7:our site is http://www.etiantian.org
9:my qq num is 49000488.
11:not 4900000448.
12:my god,i am not oldbey,but OLDBOY
13:oldb y
  • 匹配任意单个字符
[root@linux-node1 ~]# grep "." linbin.txt  ##不匹配空行
I am oldboy teacher!
I teach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y

[root@linux-node1 ~]# grep "oldb.y" linbin.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god,i am not oldbey,but OLDBOY
oldb y
  • 转义符
[root@linux-node1 ~]# grep "\.$" linbin.txt
I teach linux.
I like badminton ball,billard ball and chinese chess.
my qq num is 49000488.
not 4900000448.
  • 匹配前面项0次或多次
[root@linux-node1 ~]# grep "0*" linbin.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billard ball and chinese chess.

my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org

my qq num is 49000488.

not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y

[root@linux-node1 ~]# grep -o "0*" linbin.txt  ##精确匹配字符串
000
00000
  • 匹配任意字符
[root@linux-node1 ~]# grep ".*" linbin.txt  ##匹配空行
I am oldboy teacher!
I teach linux.

I like badminton ball,billard ball and chinese chess.

my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org

my qq num is 49000488.

not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
  • 匹配集合以内的任意单个字符
[root@linux-node1 ~]# grep "[abc]" linbin.txt
I am oldboy teacher!
I teach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my god,i am not oldbey,but OLDBOY
oldb y

[root@linux-node1 ~]# grep "[0-9]" lb.txt
my blog is http://oldboy.blog.51cto.com
my qq num is 49000488.
not 4900000448.ss
  • 匹配集合以外的任意单个字符
[root@linux-node1 ~]# grep "[^a-z]" linbin.txt  ##匹配非小写字母
I am oldboy teacher!
I teach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y

[root@linux-node1 ~]# grep "[^A-Z]" linbin.txt  ##匹配非大写字母
I am oldboy teacher!
I teach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y

[root@linux-node1 ~]# grep "[^0-9]" linbin.txt  ##匹配非数字
I am oldboy teacher!
I teach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y

扩展正则表达式(Extended Regular Expression)

序号 符号 描述 示例
1 + 匹配前面项1次或多次 ab+匹配ab或abb,不匹配a
2 ? 匹配前面项0次或1次 ab?匹配a或ab,不匹配abb
3 匹配 两边的任意一项 ab cd匹配ab或cd
4 () 匹配表达式 a(c d)e匹配ace或ade,不匹配ae
5 {n,m} 匹配前面项n~m次 ab{2,3}匹配abb或abbb,需要对{}进行转义
6 {n,} 匹配前面项至少n次,包含n次 ab{2,}匹配abb或abbb,需要对{}进行转义
7 {n} 匹配前面项n次 ab{2}匹配abb,需要对{}进行转义
8 {,m} 匹配前面项最多m次,包含m次 ab{,2}匹配a或ab或abb,需要对{}进行转义
  • 数据模拟
[root@linux-node1 ~]# vim linbin.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billard ball and chinese chess.

my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org

my qq num is 49000488.

not 4900000448.
my god,i am not oldbey,but OLDBOY
good
goood
gd
  • 匹配前面项1次或多次
[root@linux-node1 ~]# grep -E "go+d" linbin.txt
my god,i am not oldbey,but OLDBOY
good
goood
  • 匹配前面项0次或1次
[root@linux-node1 ~]# grep -E "go?d" linbin.txt
my god,i am not oldbey,but OLDBOY
gd
  • 匹配|两边的任意一项
[root@linux-node1 ~]# grep -E "god|good" linbin.txt
my god,i am not oldbey,but OLDBOY
good
  • 匹配表达式
[root@linux-node1 ~]# grep -E "g(la|oo)d" linbin.txt
good
[root@linux-node1 ~]# egrep "g(la|oo)d" linbin.txt
good
[root@linux-node1 ~]# egrep "g(oo)?d" linbin.txt
good
gd
  • 匹配前面项n~m次
[root@linux-node1 ~]# grep -E "0{3,5}" linbin.txt
my qq num is 49000488.
not 4900000448.
  • 匹配前面项至少n次
[root@linux-node1 ~]# grep -E "0{3,}" linbin.txt
my qq num is 49000488.
not 4900000448.
  • 匹配前面项n次
[root@linux-node1 ~]# grep -E "0{3}" linbin.txt
my qq num is 49000488.
not 4900000448.

Linux正则表达式

标签:egrep   扩展   strong   ade   into   ext   root   符号   vim   

原文地址:http://blog.51cto.com/11097612/2068087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!