Linux学习基础之Shell编程——正则表达式

小编 2026-06-23 阅读:1007 评论:0
1、正则表达式与通配符 》正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配。grep、awk、sed等命令可以支持正则表达式。 》通配符用来匹配符合条件的文件名,通配符是完全匹配。ls、fin...

1、正则表达式与通配符

》正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配。grep、awk、sed等命令可以支持正则表达式。

》通配符用来匹配符合条件的文件名,通配符是完全匹配。ls、find、cp等这些命令不支持正则表达式,所以只能使用shell自己的通配符来进行匹配了。

注:Linux中正则和通配符的区别比较大,其他编程语言中,通配符属于正则中的一部分。

2、基础正则表达式:

元字符 作用
* 前一个字符匹配0次或任意多次
. 匹配除了换行符外任意一个字符
^ 匹配行首。例如:^hello 会匹配以hello开头的行
$ 匹配行尾。例如:hello& 会匹配以hello结尾的行
[ ] 匹配中括号中指定的任意一个字符,只匹配一个字符。例如:[aoeiu]匹配任意一个元音字母,[0-9]匹配任意一位数字,[a-z][0-9]匹配小写字母和一位数字构成的两位字符
[^  ] 匹配除中括号的字符以外的任意一个字符。例如:[^0-9]匹配任意一位非数字字符,[^a-z]匹配任意一位非小写字母
\\ 转义符。用于将特殊符号的含义取消
\\{n\\} 表示其前面的字符恰好出现n次。例如:[0-9]\\{4\\}匹配4位数字,[1][3,8][0-9]\\{9\\}匹配手机号码
\\{n , \\} 表示其前面的字符出现不小于n次。例如:[0-9]\\{2,\\}表示两位及以上的数字
\\{n , m \\} 表示其前面的字符至少出现n次,最多出现m次。例如:[a-z]\\{6,8\\}匹配6到8位的小写字母

注意:

1)通配符中的* 号和正则中的* 号有区别;

2)正则中的. 相当于通配符中的?

如下测试文档内容:

[root@localhost sh]# vim zhengzetest

Mr. XiaoxiaoZhou said:

he was the honest man in his School.

123 despise him.

But since Mr. Li lei came,
he never saaaid those words.
5555nice!

because,actunaaaally,
Mr. Li lei is teh most honest man!

Later,Mr. XiaoxiaoZhou sold his hot body.


~                                          

示例1、

“ * ”   前一个字符匹配0次,或任意多次

》grep \"a*\" test_rules.txt

#匹配所有内容,包括空白行

》grep \"aa*\" test_rules.txt

#匹配至少包含有一个a的行

》grep \"aaa*\" test_rules.txt

#匹配最少包含两个连续a的字符

》grep \"aaaaa*\" test_rules.txt

#则会匹配最少包含四个连续a的字符串

[root@localhost sh]# grep \"a*\" zhengzetest 
Mr. XiaoxiaoZhou said:

he was the honest man in his School.

123 despise him.

But since Mr. Li lei came,
he never saaaid those words.
5555nice!

because,actunaaaally,
Mr. Li lei is teh most honest man!

Later,Mr. XiaoxiaoZhou sold his hot body.


[root@localhost sh]# grep \"aa*\" zhengzetest 
Mr. XiaoxiaoZhou said:
he was the honest man in his School.
But since Mr. Li lei came,
he never saaaid those words.
because,actunaaaally,
Mr. Li lei is teh most honest man!
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# grep \"aaa*\" zhengzetest 
he never saaaid those words.
because,actunaaaally,
[root@localhost sh]# grep \"aaaaa*\" zhengzetest 
because,actunaaaally,
[root@localhost sh]# 

示例2、“ . ” 匹配除了换行符外任意一个字符

》grep \"s..d\" test_rules.txt

#\"s..d\" 会匹配在s和d这两个字母之间一定有两个字符的单词

》grep \"s.*d\" test_rules.txt

#匹配在s和d字母之间有任意字符

》grep \".*\" test_rules.txt

#匹配所有内容

[root@localhost sh]# grep \"s..d\" zhengzetest 
Mr. XiaoxiaoZhou said:
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# grep \"s.*d\" zhengzetest 
Mr. XiaoxiaoZhou said:
he never saaaid those words.
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# grep \".*\" zhengzetest 
Mr. XiaoxiaoZhou said:

he was the honest man in his School.

123 despise him.

But since Mr. Li lei came,
he never saaaid those words.
5555nice!

because,actunaaaally,
Mr. Li lei is teh most honest man!

Later,Mr. XiaoxiaoZhou sold his hot body.


[root@localhost sh]# 

示例3、“^”匹配行首,“$”匹配行尾

》grep \"^M\" zhengzetest

#匹配以大写“M” 开头的行

》grep “n$”  zhengzetest

#匹配以小写“n”结尾的行

》grep -n \"^$\" zhengzetest

#会匹配空白行

[root@localhost sh]# grep \"^M\" zhengzetest 
Mr. XiaoxiaoZhou said:
Mr. Li lei is teh most honest man!
[root@localhost sh]# grep \"n$\" zhengzetest 
[root@localhost sh]# grep -n \"^$\" zhengzetest 
2:
4:
6:
10:
13:
15:
16:
[root@localhost sh]# vim zhengzetest
[root@localhost sh]# grep \"e$\" zhengzetest 
[root@localhost sh]# vim zhengzetest
[root@localhost sh]# grep \",$\" zhengzetest 
But since Mr. Li lei came,
because,actunaaaally,
[root@localhost sh]# 

示例4、“[  ]”匹配中括号中指定的任意一个字符,只匹配一个字符

》grep \"s[ao]id\" zhengzetest

#匹配s和i字母中,要不是a,要不是o

》grep“[0-9]” zhengzetest

#匹配任意一个数字

》grep \"^[a-z]\" zhengzetest

#匹配用小写字母开头的行

[root@localhost sh]# 
[root@localhost sh]# grep \"s[ao]id\" zhengzetest 
Mr. XiaoxiaoZhou said:
[root@localhost sh]# grep \"[0-9]\" zhengzetest 
123 despise him.
5555nice!
[root@localhost sh]# grep \"^[a-z]\" zhengzetest 
he was the honest man in his School.
he never saaaid those words.
because,actunaaaally,
[root@localhost sh]# 

示例5、“[^ ]”匹配除中括号内的字符以外的任意一个字符

》grep \"^[^a-z]\" zhengzetest

#匹配不用小写字母开头的行

》grep \"^[^a-zA-Z]\" zhengzetest

#匹配不用字母开头的行

[root@localhost sh]# grep \"^[^a-z]\" zhengzetest 
Mr. XiaoxiaoZhou said:
123 despise him.
But since Mr. Li lei came,
5555nice!
Mr. Li lei is teh most honest man!
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# grep \"^[^a-zA-Z\" zhengzetest 
grep:  不匹配的 [ 或 [^
[root@localhost sh]# grep \"^[^a-zA-Z]\" zhengzetest 
123 despise him.
5555nice!
[root@localhost sh]# 

示例6、“ \\”转义符

》grep \"\\.$\" zhengzetest     

#匹配使用“ . ” 结尾的行   因为. 在正则表达式或者说shell中有特殊含义,想要使用

其最普通的英文句号的功能,只能用转义符将其在shell中的特殊含义去掉,再用

[root@localhost sh]# grep \"\\.$\" zhengzetest 
he was the honest man in his School.
123 despise him.
he never saaaid those words.
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# 

示例7、“\\{n\\}” 表示其前面的字符恰好出现n次

》grep \"a\\{3\\}\" zhengzetest

#匹配a字母连续出现三次的字符串

》grep \"[0-9]\\{3}\" zhengzetest

#匹配包含连续的三个数字的字符串

[root@localhost sh]# grep \"a\\{3\\}\" zhengzetest 
he never saaaid those words.
because,actunaaaally,
[root@localhost sh]# grep \"[0-9]\\{3\\}\" zhengzetest 
123 despise him.
5555nice!
[root@localhost sh]# 

示例8、“\\{n,m\\}” 匹配其前面的字符至少出现n次最多出现m次

》grep \"sa\\{1,3\\}i\" zhengzetest

#匹配在字母s和字母i之间有最少一个a,最多三个a

[root@localhost sh]# grep \"sa\\{1,3\\}\" zhengzetest 
Mr. XiaoxiaoZhou said:
he never saaaid those words.

 

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表