正则表达式的使用
在Shell命令行中,用户可以使用grep命令来测试
命令名 | 支持正则表达式类型 |
---|---|
grep | 支持使用基本正则表达式 |
egrep | 支持使用扩展正则表达式 |
fgrep | 不支持使用正则表达式,即所有的正则表达式中的元字符都将作为一般字符,仅仅拥有其字面意义,不再拥有特殊意义 |
Grep命令参数解析
参数 | 解析 |
---|---|
-n | 显示行号 |
-o | 只显示匹配的内容 |
-q | 静默模式,没有任何输出,得用$?来判断执行成功没有,即有没有过滤到想要的内容 |
-l | 如果匹配成功,则只将文件名打印出来,失败则不打印,通常-rl一起用,grep -rl ‘root’ /etc |
-A | 如果匹配成功,则将匹配行及其后n行一起打印出来 |
-B | 如果匹配成功,则将匹配行及其前n行一起打印出来 |
-C | 如果匹配成功,则将匹配行及其前后n行一起打印出来 |
–color | 高亮颜色显示匹配到的字符串 |
-c | 如果匹配成功,则将匹配到的行数打印出来 |
-E | 等于egrep,扩展 |
-i | 忽略大小写 |
-v | 取反,不匹配 |
-w | 匹配单词 |
-r | 递归搜索,不仅搜索当前目录,还要搜索其各级子目录 |
-s | 不显示关于不存在或者无法读取文件的错误信息 |
测试用例:
[root@haha day6]# grep -n '"' test_file
1:fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
3:"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
5:"I know every day that buying books, buying books, what else will I do besides buying books!" The wife was angry and yelling, pulled out the chair, gave a hard meal, and sat down on it.
7:"I'm blind. When I was young, I thought you could read a word, but I thought you could do something big!"
[root@haha day6]#
[root@haha day6]# grep -no '"' test_file
1:"
1:"
1:"
1:"
1:"
1:"
3:"
[root@haha day6]# grep -noq '"' test_file
[root@haha day6]# echo $?
0
[root@haha day6]# grep -nol '"' test_file
test_file
[root@haha day6]# grep -A 3 fter test_file
fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
[root@haha day6]# grep -B 2 Buy test_file
fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
[root@haha day6]# grep -C 2 Buy test_file
fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
"I know every day that buying books, buying books, what else will I do besides buying books!" The wife was angry and yelling, pulled out the chair, gave a hard meal, and sat down on it.
[root@haha day6]# grep -c I test_file
9
[root@haha day6]# grep -v I test_file
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
"Not bought, borrowed? Who still buys books to read now? Also lend it to you! Hey! " My wife shrugged me off and stood up and went out.
[root@haha day6]# grep -w Buy test_file
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
[root@haha day6]# grep -i buy test_file
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
"I know every day that buying books, buying books, what else will I do besides buying books!" The wife was angry and yelling, pulled out the chair, gave a hard meal, and sat down on it.
"No, I didn't buy it …".
"Not bought, borrowed? Who still buys books to read now? Also lend it to you! Hey! " My wife shrugged me off and stood up and went out.
基本正则表达式
基本正则表达式(Basic Regular Expression,BRE),又称为标准正则表达式,是最早制订的正则表达
式规范,仅支持最基本的元字符集。基本正则表达式是POSIX规范制订的两种正则表达式语法标准之
一。
字符 | 含义 |
---|---|
^ | 在每行的开始进行匹配 |
$ | 在每行的末尾进行匹配 |
. | 对任何单个字符进行匹配 |
* | 对前一项进行0次或多次重复匹配 |
[str] | 对str中的任何单个字符进行匹配 |
[^str] | 对任何不在str中的单个字符进行匹配 |
[a-b] | 对a到b之间的任何字符进行匹配 |
\ | 忽略后面一个字符的特殊含义 |
测试用例:
[root@haha day6]# grep -i '^"buy' test_file
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
[root@haha day6]# grep -i 'table.$' test_file
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
[root@haha day6]# grep -i ^.*$ test_file
fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
"Buy books again!" With a loud drink, my wife snatched the book from my hand and slammed it on the table.
"I know every day that buying books, buying books, what else will I do besides buying books!" The wife was angry and yelling, pulled out the chair, gave a hard meal, and sat down on it.
[root@haha day6]# egrep -w [.] test_file
fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
"No, I didn't buy it …".
[root@haha day6]# egrep [a-c] test_file
fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
正则表达式字符集
字符 | 说明 |
---|---|
[[:alnum:]] | 匹配任意一个字母或者数字,等价于[A-Za-z0-9] |
[[:alpha:]] | 匹配任意一个字母,等价于[A-Za-z] |
[[:digit:]] | 匹配任意一个数字,等价于0-9 |
[[:lower:]] | 匹配任意一个小写字母,等价于a-z |
[[:upper:]] | 匹配任意一个大写字母,等价于A-Z |
[[:space:]] | 匹配任意一个空白符,包括空格、制表符、换行符以及分页符 |
[[:blank:]] | 匹配空格和制表符 |
[[:graph:]] | 匹配任意一个看得见的可打印字符,不包括空白字符 |
[[:print:]] | 匹配任何一个可以打印的字符,包括空白字符,但是不包括控制字符、字符串结束符‘\0’、EOF文件结束符(-1) |
[[:cntrl:]] | 匹配任何一个控制字符,即ASCII字符集中的前32个字符。例如换行符、制表符等 |
[[:punct:]] | 匹配任何一个标点符号,例如“[]”、“{}”或者“,”等 |
[[:xdigit:]] | 匹配十六进制数字,即0-9、a-f以及A-F |
测试用例:
[root@haha day6]# egrep [[:upper:]] test_file
fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
[root@haha day6]# egrep [[:xdigit:]] test_file
fter receiving the book Nostalgia and Rhyme, I rubbed it slowly, "My article has finally been printed", and I couldn't help crying, "I have been writing for more than ten years, …". Open the table of contents and find my article, "Oh, on page 58". My hand trembled slightly.
扩展正则表达式
扩展正则表达式(Extended Regular Expression,ERE)支持比基本正则表达式更多的元字符,但是扩
展正则表达式对有些基本正则表达式所支持的元字符并不支持。前面介绍的元字符“^”、“$”、“.”、“*”、
“[]”以及“[^]”这6个元字符在扩展正则表达式都得到了支持,并且其意义和用法都完全相同。
字符 | 含义 |
---|---|
+ | 对前一项进行1次或多次重复匹配 |
? | 对前一项进行0次或1次重复匹配 |
{j} | 对前一项进行j次重复匹配 |
{j,} | 对前一项进行j次或更多次重复匹配 |
{,k} | 对前一项最多进行k次重复匹配 |
(s|t) | 匹配s项或t项中的一项 |
测试用例:
[root@haha day6]# egrep -q ^.+$ test_file
[root@haha day6]# echo $?
0
案例
- 匹配学号
[root@haha day6]# echo 04101802043 | egrep ^"041018020([0][1-9]|[1-3][0-9]|[4][0-3])"$
04101802043
- ipv4地址
[root@haha day6]# echo 12.122.12.12 | grep -oP '(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))'
12.122.12.12
- 邮箱地址
[root@haha day6]# echo lc122547@163.com | grep -oP ^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$
lc122547@163.com
- 8位强密码
[root@haha day6]# echo Lc12254@ |grep -oP '(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,}'
Lc12254@
- url正则表达式
[root@haha day6]# echo https://www.cnblogs.com/eliwen/p/12742421.html | grep -oP "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"
https://www.cnblogs.com/eliwen/p/12742421.html