|NO.Z.00002|——————————|LinuxShell|——|Linux&Shell&正则表达式.V02|
一、“[]” 匹配中括号中指定的任意一个字符,只匹配一个字符
### --- 匹配一个 o 字符:
~~~ “[]”会匹配中括号中指定任意一个字符,注意只能匹配一个字符。
~~~ 比如[ao]要不会匹配一个 a 字符,要不会
[root@localhost ~]# grep "s[ao]id" test_rule.txt
Mr. Li Ming said:
Later,Mr. Li ming soid his hot body.
### --- 而“[0-9]”会匹配任意一个数字,如:
[root@localhost ~]# grep "[0-9]" test_rule.txt
123despise him.
5555nice!
### --- 而“[A-Z]”则会匹配一个大写字母,如:
[root@localhost ~]# grep "[A-Z]" test_rule.txt
Mr. Li Ming said:
But since Mr. shen Chao came,
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
### --- 如果正则是“^[a-z]”代表匹配用小写字母开头的行:
[root@localhost ~]# grep "^[a-z]" test_rule.txt
he was the most honest man.
he never saaaid those words.
because,actuaaaally,
二、“[^]” 匹配除中括号的字符以外的任意一个字符
### --- “[^]” 匹配除中括号的字符以外的任意一个字符
[root@localhost ~]# grep "^[^a-z]" test_rule.txt
Mr. Li Ming said:
123despise him.
But since Mr. shen Chao came,
5555nice!
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
### --- 而“^[^a-zA-Z]”则会匹配不用字母开头的行:
[root@localhost ~]# grep "^[^a-zA-Z]" test_rule.txt
123despise him.
5555nice!
三、“\” 转义符
### --- 转义符
[root@localhost ~]# grep "\.$" test_rule.txt
he was the most honest man.
123despise him.
he never saaaid those words.
Later,Mr. Li ming soid his hot body.
四、“\{n\}”表示其前面的字符恰好出现 n 次
### --- “\{n\}”表示其前面的字符恰好出现 n 次
[root@localhost ~]# grep "a\{3\}" test_rule.txt
he never saaaid those words.
because,actuaaaally,
### --- 上面的两行都包含三个连续的 a,所以都会匹配。
### --- 但是如果先要只显示三个连续的 a,可以这样来写正则:
[root@localhost ~]# grep "[su]a\{3\}[il]" test_rule.txt
he never saaaid those words.
### --- 如果正则是“[0-9]\{3\}”则会匹配包含连续的三个数字的字符串:
~~~ 注:虽然“5555”有四个连续的数字,但是包含三个连续的数字,
~~~ 所以也是可以列出的。可是这样不
~~~ 注: 能体现出来“[0-9]\{3\}”只能匹配三个连续的数字,
~~~ 而不能匹配四个连续的数字。那么正则就应该
[root@localhost ~]# grep "[0-9]\{3\}" test_rule.txt
123despise him.
5555nice!
### --- 这样来写“^[0-9]\{3\}[a-z]”:
~~~ 注:只匹配用连续三个数字开头的行
[root@localhost ~]# grep "^[0-9]\{3\}[a-z]" test_rule.txt
123despise him.
五、“\{n,\}”表示其前面的字符出现不小于 n 次
### --- “\{n,\}”表示其前面的字符出现不小于 n 次
~~~ “\{n,\}”会匹配前面的字符出现最少 n 次。
~~~ 比如“zo\{3,\}m”这个正则就会匹配用 z 开头,m
~~~ 结尾,中间最少有三个 o 的字符串。
~~~ 那么“^[0-9]\{3,\}[a-z]”这个正则就能匹配最少用连续三个
### --- 1/数字开头的字符串:
[root@localhost ~]# grep "^[0-9]\{3,\}[a-z]" test_rule.txt
123despise him.
5555nice!
### --- 而“[su]a\{3,\}[il]”正则则会匹配在字母 s 或 u 和 i 或 l 之间,
~~~ 最少出现三个连续的 a 的字符串:
[root@localhost ~]# grep "[su]a\{3,\}[il]" test_rule.txt
he never saaaid those words.
because,actuaaaally,
六、“\{n,m\}”匹配其前面的字符至少出现 n 次,最多出现 m 次
### --- “\{n,m\}”匹配其前面的字符至少出现 n 次,最多出现 m 次
[root@localhost ~]# grep "sa\{1,3\}i" test_rule.txt
Mr. Li Ming said:
he never saaaid those words.
### --- 匹配在字母 s 和字母 i 之间有最少一个 a,最多三个 a
[root@localhost ~]# grep "sa\{2,3\}i" test_rule.txt
he never saaaid those words.
### --- 匹配在字母 s 和字母 i 之间有最少两个 a,最多三个 a
[root@localhost ~]# grep "sa\{2,3\}i" test_rule.txt
he never saaaid those words.
一、扩展正则表达式
### --- 扩展正则表达式
~~~ 熟悉正则表达式的童鞋应该很疑惑,在正则表达式中应该还可以支持一些元字符,
~~~ 比如“+”“?”“|”“()”。其实 Linux 是支持这些元字符的,只是 grep 命令默认不支持而已。
~~~ 如果要想支持这些元字符,必须使用 egrep 命令或 grep -E 选项,
~~~ 所以我们又把这些元字符称作扩展元字符。
~~~ 如果查询 grep 的帮助,对 egrep 的说明就是和 grep -E 选项一样的命令,
~~~ 所以我们可以把两个命令当做别名来对待。
二、通过表 12-2 来看看 Shell 中支持的扩展元字符:
作用 | |
+ | 前一个字符匹配1次或任意多次。如“go+gle”会匹配“google”, “google”或“google”当然如果“0”有更多个,也能匹配 |
? | 前一个字符匹配,0次或1次 |
如“colour”可以匹配“colour”或“color” | |
| | 匹配两个或多个分支选择, 如“washis”会匹配既包含“was”的行,也匹配包含his的行 |
() | 匹配其整体为一个字符,即模式单元,可以理解为由多个单个字符组成的打字符; 如(dog)+会匹配dog,dogdog,dogdogdog等, 因为被()包含的字符会当成一个整体但“hello(worldearth)会匹配hello world及hello earth |
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
cdv008-shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通