当前位置:网站首页>Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
2022-07-27 09:49:00 【Redamancy06】
文章目录
1.正则表达式入门
正则表达式使用单个字符串来描述、匹配一系列符合某个语法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。在 Linux 中,grep,sed,awk 等文本处理工具都支持通过正则表达式进行模式匹配。
1.1常规匹配
一串不包含特殊字符的正则表达式匹配它自己,例如:
[root[email protected] ~]# cat /etc/passwd | grep 00:
就会匹配所有包含 00:的行。
1.2常用特殊字符
1.2.1特殊字符:^
^ 匹配一行的开头,例如:
会匹配出所有以 a 开头的行
1.2.2特殊字符:$
$ 匹配一行的结束,例如
会匹配出所有以 h结尾的行
- 思考:^$ 匹配什么?
匹配空行

daily_archive.sh文件是在http://t.csdn.cn/0Wpml 写的,想看的点这里看里面的内容
1.2.3特殊字符:.
匹配一个任意的字符,例如
1.2.4特殊字符:*
“*”不单独使用,他和上一个字符连用,表示匹配上一个字符 0 次或多次,例如
[[email protected] ~]# cat /etc/passwd | grep ro*t
会匹配 rt, rot, root, rooot, roooot 等所有行
思考:.* 匹配什么?
匹配任意字符,可以和^$连用,放在他们中间用,就可以将下面的代码实现,以a开头,login结尾的行
[[email protected] ~]# cat /etc/passwd | grep ^a.*login$

[[email protected] ~]# cat /etc/passwd | grep ^a.*var.*login$
1.2.5字符区间(中括号):[ ]
[ ] 表示匹配某个范围内的一个字符,例如
[6,8]------匹配 6 或者 8
[0-9]------匹配一个 0-9 的数字
[0-9]------匹配任意长度的数字字符串
[a-z]------匹配一个 a-z 之间的字符
[a-z] ------匹配任意长度的字母字符串
[a-c, e-f]-匹配 a-c 或者 e-f 之间的任意字符

[[email protected] ~]# cat /etc/passwd | grep r[a-z]*t
1.2.6特殊字符:\
\ 表示转义,并不会单独使用。由于所有特殊字符都有其特定匹配模式,当我们想匹配某一特殊字符本身时(例如,我想找出所有包含 ‘$’ 的行),就会碰到困难。此时我们就要将转义字符和特殊字符连用,来表示特殊字符本身,例如

就会匹配所有包含 $ 的行。注意需要使用单引号将表达式引起来。
1.2.7匹配手机号

[[email protected] scripts]# echo “13812345678” | grep ^1[34578][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$

[[email protected] scripts]# echo “13812345678” | grep -E ^1[34578][0-9]{9}$
记得加大写的-E,因为目前grep不支持{9}的扩展的正则的,所以需要加上-E,表示支持扩展的正则表达式
边栏推荐
- Towards the peak of life
- Case of burr (bulge) notch (depression) detection of circular workpiece
- Brush the title "sword finger offer" day03
- 学习Typescript(一)
- 达梦 PARTGROUPDEF是自定义的对象吗?
- 交换机端口镜像配置指南
- How to restore the original version after installing Hal Library
- 吃透Chisel语言.27.Chisel进阶之有限状态机(一)——基本有限状态机(Moore机)
- 中高级试题」:MVCC 实现原理是什么?
- July training (day 09) - two point search
猜你喜欢

35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction

如何在树莓派上安装cpolar内网穿透

电机控制器中的MOS驱动

拜托!面试请不要再问我 Ribbon 的架构原理

食品安全 | 垃圾食品越吃越想吃?这份常见食品热量表请收好

GBase 8a MPP集群扩容实战
![Flash memory usage and stm32subemx installation tutorial [day 3]](/img/ff/43ef2d0e1bdfd24fbc5c99e76455f1.png)
Flash memory usage and stm32subemx installation tutorial [day 3]

面试京东 T5,被按在地上摩擦,鬼知道我经历了什么?

Brush the title "sword finger offer" day04

XML概述
随机推荐
July training (day 03) - sorting
[SCM]源码管理 - perforce 分支的锁定
并发之park与unpark说明
Expose a technology boss from a poor family
July training (day 13) - two way linked list
July training (day 20) - binary search tree
wordpress禁止指定用户名登录或注册插件【v1.0】
聊聊索引失效的10种场景,太坑了
July training (day 12) - linked list
Towards the peak of life
Understand chisel language. 22. Chisel sequential circuit (II) -- detailed explanation of chisel counter: counter, timer and pulse width modulation
July training (day 14) - stack
How to install cpolar intranet penetration on raspberry pie
[scm] source code management - lock of perforce branch
Understand chisel language. 23. Chisel sequential circuit (III) -- detailed explanation of chisel shift register
Flash memory usage and stm32subemx installation tutorial [day 3]
食品安全 | 垃圾食品越吃越想吃?这份常见食品热量表请收好
Understand chisel language. 24. Chisel sequential circuit (IV) -- detailed explanation of chisel memory
Engineering survey simulation volume a
Meeting seating function of conference OA project & Implementation of meeting submission for approval