Linux正则表达式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux正则表达式相关的知识,希望对你有一定的参考价值。
基础正则表达式(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 |
- 数据模拟
[[email protected] ~]# 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
- 匹配以……开头的行
[[email protected] ~]# 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
- 匹配以……结尾的行
[[email protected] ~]# grep "m$" linbin.txt
my blog is http://oldboy.blog.51cto.com
- 匹配空行
[[email protected] ~]# 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
- 匹配任意单个字符
[[email protected] ~]# 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
[[email protected] ~]# 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
- 转义符
[[email protected] ~]# grep "\.$" linbin.txt
I teach linux.
I like badminton ball,billard ball and chinese chess.
my qq num is 49000488.
not 4900000448.
- 匹配前面项0次或多次
[[email protected] ~]# 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
[[email protected] ~]# grep -o "0*" linbin.txt ##精确匹配字符串
000
00000
- 匹配任意字符
[[email protected] ~]# 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
- 匹配集合以内的任意单个字符
[[email protected] ~]# 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
[[email protected] ~]# grep "[0-9]" lb.txt
my blog is http://oldboy.blog.51cto.com
my qq num is 49000488.
not 4900000448.ss
- 匹配集合以外的任意单个字符
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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,需要对{} 进行转义 |
- 数据模拟
[[email protected] ~]# 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次或多次
[[email protected] ~]# grep -E "go+d" linbin.txt
my god,i am not oldbey,but OLDBOY
good
goood
- 匹配前面项0次或1次
[[email protected] ~]# grep -E "go?d" linbin.txt
my god,i am not oldbey,but OLDBOY
gd
- 匹配|两边的任意一项
[[email protected] ~]# grep -E "god|good" linbin.txt
my god,i am not oldbey,but OLDBOY
good
- 匹配表达式
[[email protected] ~]# grep -E "g(la|oo)d" linbin.txt
good
[[email protected] ~]# egrep "g(la|oo)d" linbin.txt
good
[[email protected] ~]# egrep "g(oo)?d" linbin.txt
good
gd
- 匹配前面项n~m次
[[email protected] ~]# grep -E "0{3,5}" linbin.txt
my qq num is 49000488.
not 4900000448.
- 匹配前面项至少n次
[[email protected] ~]# grep -E "0{3,}" linbin.txt
my qq num is 49000488.
not 4900000448.
- 匹配前面项n次
[[email protected] ~]# grep -E "0{3}" linbin.txt
my qq num is 49000488.
not 4900000448.
以上是关于Linux正则表达式的主要内容,如果未能解决你的问题,请参考以下文章
正则表达式匹配特定的 URL 片段而不是所有其他 URL 可能性