
云计算 Linux 课程系列
—————————————————————————————
3)、 “.” 匹配除了换行符外任意一个字符
正则表达式“.”只能匹配一个字符,这个字符可以是任意字符,举个例子:
[root@localhost ~]# grep "s..d" test_rule.txt
Mr. Li Ming
:
Later,Mr. Li ming
his hot body.
#“s..d”会匹配在 s 和 d 这两个字母之间一定有两个字符的单词
[root@localhost ~]# grep "s.*d" test_rule.txt
Mr. Li Ming
:
he never
those words.
Later,Mr. Li ming
y.
#最后一句话比较有意思,匹配的是“
”
[root@localhost ~]# grep ".*" test_rule.txt
4)、 “^”匹配行首,“$”匹配行尾
“^”代表匹配行首,比如“^M”会匹配以大写“M”开头的行:
[root@localhost ~]# grep "^M" test_rule.txt
r. Li Ming said:
r. Shen Chao is the most honest man
“$”代表匹配行尾,如果“n$”会匹配以小写“n”结尾的行:
[root@localhost ~]# grep "n$" test_rule.txt
Mr. Shen Chao is the most honest man
而“^$”则会匹配空白行:
[root@localhost ~]# grep -n "^$" test_rule.txt
5)、 “[]” 匹配中括号中指定的任意一个字符,只匹配一个字符
“[]”会匹配中括号中指定任意一个字符,注意只能匹配一个字符。比如[ao]要不会匹配一个 a
字符,要不会匹配一个 o 字符:
[root@localhost ~]# grep "s[ao]id" test_rule.txt
而“[0-9]”会匹配任意一个数字,如:
[root@localhost ~]# grep "[0-9]" test_rule.txt
而“[A-Z]”则会匹配一个大写字母,如:
[root@localhost ~]# grep "[A-Z]" test_rule.txt
如果正则是“^[a-z]”代表匹配用小写字母开头的行:
[root@localhost ~]# grep "^[a-z]" test_rule.txt
6)“[^]” 匹配除中括号的字符以外的任意一个字符
[root@localhost ~]# grep "^[^a-z]" test_rule.txt
而“^[^a-zA-Z]”则会匹配不用字母开头的行:
[root@localhost ~]# grep "^[^a-zA-Z]" test_rule.txt
更多云计算-Java –大数据 –前端 –python 人工智能资料下载,可百度访问:尚硅谷官网
文档被以下合辑收录
相关文档
评论