zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux正则表达式

2023-09-27 14:26:42 时间
正则表达式分类:
1、基本正则表达式(BRE,Basic Regular Expression) 
BRE对应的元字符有 "^%.[]*"
2、扩展正则表达式(ERE,Extended Regular Expression) 
ERE在BRE的基础上增加了 "(){}?+|" 等

说明:支持扩展正则的3中方法:
(1)grep命令加"-E"参数。
(2)grep命令不加参数也可以使用扩展正则表达式的特殊字符,但是需要在使用每个特殊的字符前面加“\”(反斜线)。
(3)egrep命令可以直接支持扩展正则。

基本正则表达式(BRE)集合
^       用法为: "^abc",表示匹配以abc单词开头的行
$       用法为: "abc$",表示匹配以abc单词结尾的行
^$      表示空行,以"^"结尾的行,或者以"$"开头的行
.       表示匹配任意一个且只有一个字符(但是不能匹配空行)
\       转移字符,让有特殊含义的字符表达原来的含义,如"\." 只表示小数点
*       匹配前一个字符(连续出现)0次或者1次以上。注意,当重复0次的时候,表示什么也没有(空),即匹配所有内容
.*      匹配所有内容
^.*     匹配以任意多个字符开头的内容
.*$     匹配以任意多个字符结尾的内容
[abc]   匹配"[]"集合内任意一个字符a或b或c,[abc]也可以写成[a-c]
[^abc]  匹配不包含"^"后的任意字符a或b或c,此处的"^"不能用"!"替代


扩展正则表达式(ERE)集合
+       匹配前一个字符1次或多次
[:/]+   匹配括号内的":""/"字符一次或多次
?       匹配前一个字符0次或1次
|       表示或者,即同时过滤多个字符串
()      分组过滤,被括起来的内容表示一个整体,另外"()"的内容可以被后面的"\n"引用,n为数字,表示引用第几个括号的内容。
\n      引用前面"()"里面的内容,例如,(aa)\1,匹配aaaa
a{n,m}  匹配前一个字符最少n次,最多m次
a{n,}   匹配前已给字符最少n次
a{n}    匹配前一个字符刚好n次
a{,m}   匹配前一个字符最多m次


基本正则表达式实践
实践环境准备
mkdir ~/test -p
cat >~/test/guojun.txt <<EOF
I am guojun teacher!
I teach Database.

I like badminton ball ,billiard ball and chinese chess!
our site is https://www.cnblogs.com
my qq num is 12000345.

not 1200000345.
my god ,i am not guojnn,but GUOJUN!
EOF


范例:"^" 功能实践
输出以m开头的所有行并打印行号
[root@testdb62 test]# grep -n "^m" guojun.txt
6:my qq num is 12000345.
9:my god ,i am not guojnn,but GUOJUN!


范例:"$" 功能实践
输出以m结尾的所有行
[root@testdb62 test]# grep  "m$" guojun.txt
our site is https://www.cnblogs.com


范例:"$" 功能实践
显示空行及对应的行号
[root@testdb62 test]# grep -n "^$" guojun.txt 
3:
7:


范例:"." 功能实践
匹配任意一个字符并输出对应文件中的行号
[root@testdb62 test]# grep -n "." guojun.txt 
1:I am guojun teacher!
2:I teach Database.
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is https://www.cnblogs.com
6:my qq num is 12000345.
8:not 1200000345.
9:my god ,i am not guojnn,but GUOJUN!


范例:".""\"功能实践
显示以"." 结尾的行
[root@testdb62 test]# grep  '\.$' guojun.txt   
I teach Database.
my qq num is 12000345.
not 1200000345.


范例:".*" 功能实践
输出文件中连续出现0次或0次以上的数字内容
[root@testdb62 test]# grep  '0*' guojun.txt    
I am guojun teacher!
I teach Database.

I like badminton ball ,billiard ball and chinese chess!
our site is https://www.cnblogs.com
my qq num is 12000345.

not 1200000345.
my god ,i am not guojnn,but GUOJUN!


范例:".*" 功能实践
通过grep显示文件的所有内容,包括空行
[root@testdb62 test]# grep  '.*' guojun.txt    
I am guojun teacher!
I teach Database.

I like badminton ball ,billiard ball and chinese chess!
our site is https://www.cnblogs.com
my qq num is 12000345.

not 1200000345.
my god ,i am not guojnn,but GUOJUN!


范例:测试 "^.*o" 能够匹配到的内容
[root@testdb62 test]# grep  '^.*o' guojun.txt     
I am guojun teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is https://www.cnblogs.com
not 1200000345.
my god ,i am not guojnn,but GUOJUN!


范例:匹配出文件中所有的大写字母
[root@testdb62 test]# grep  '[A-Z]' guojun.txt 
I am guojun teacher!
I teach Database.
I like badminton ball ,billiard ball and chinese chess!
my god ,i am not guojnn,but GUOJUN!


范例:匹配除了小写字母以外的符号
[root@testdb62 test]# grep  '[^a-z]' guojun.txt  
I am guojun teacher!
I teach Database.
I like badminton ball ,billiard ball and chinese chess!
our site is https://www.cnblogs.com
my qq num is 12000345.
not 1200000345.
my god ,i am not guojnn,but GUOJUN!


扩展正则表达式实践
范例:匹配文件中的数字0一次或多次
[root@testdb62 test]# egrep "0+" guojun.txt 
my qq num is 12000345.
not 1200000345.


范例:显示文件中包含gd或god的行
新建测试文件
mkdir ~/test -p
cat >~/test/gd.txt <<EOF
good
glad
gd
god
goood
EOF

[root@testdb62 test]# cat gd.txt 
good
glad
gd
god
goood

[root@testdb62 test]# egrep 'go?d' gd.txt 
gd
god


范例:取出包含3306或1521的行
[root@testdb62 test]#  egrep '3306|1521' /etc/services
mysql           3306/tcp                        # MySQL
mysql           3306/udp                        # MySQL
ncube-lm        1521/tcp                # nCube License Manager
ncube-lm        1521/udp                # nCube License Manager


范例:显示文件中包含good或glad的行
[root@testdb62 test]# egrep 'g(oo|la)d' gd.txt 
good
glad


范例:重复前一个字符各种次数的例子
[root@testdb62 test]# egrep "0{3,5}" guojun.txt   # 匹配数字0,3到5次
my qq num is 12000345.
not 1200000345.
[root@testdb62 test]# egrep "0{,5}" guojun.txt    # 匹配数字0,最多5次,全部输出了
I am guojun teacher!
I teach Database.

I like badminton ball ,billiard ball and chinese chess!
our site is https://www.cnblogs.com
my qq num is 12000345.

not 1200000345.
my god ,i am not guojnn,but GUOJUN!
[root@testdb62 test]# egrep "0{3,}" guojun.txt    # 匹配数字0,最少3次
my qq num is 12000345.
not 1200000345.
[root@testdb62 test]# egrep "0{3}" guojun.txt     # 匹配数字0,3次
my qq num is 12000345.
not 1200000345.



预定义特殊中括号表达式 
描述:匹配任意一个字母或者数字相当于[a-zA-Z0-9] 

[:alnum:]   匹配任意一个字母或者数字相当于[a-zA-Z0-9]  
[:alpha:]   匹配任意一个字母相当于[a-zA-Z] 
[:blank:]   匹配空格与制表符(横向与纵向)   
[:upper:]   匹配大写 
[:lower:]   匹配小写 
[:punct:]   匹配标点符号 
[:space:]   匹配包括换行符、回车在内的所有空白符  
[:digit:]   匹配任意一个数字相当于[0-9] 
[:graph:]   匹配任意一个可以看得见且可以打印的字符  
[:xdigit:]  匹配任何一个16进制数相当于[0-9][a-f][A-F] 
[:cntrl:]   任何一个控制符(ASCII字符集的前32为字符) 
[:print:]   任何一个可以打印的字符  


范例:括号表达式使用示例
[root@testdb62 test]# grep "[[:digit:]]" guojun.txt 
my qq num is 12000345.
not 1200000345.
[root@testdb62 test]# grep "[[:upper:]]" guojun.txt  
I am guojun teacher!
I teach Database.
I like badminton ball ,billiard ball and chinese chess!
my god ,i am not guojnn,but GUOJUN!