加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式练习

发布时间:2020-12-13 19:52:49 所属栏目:百科 来源:网络整理
导读:2013年3月8日 星期五 晴 有风 正则表达式练习 "Open Source" is a good mechanism to develop programs. apple is my favorite food. Football game is not use feet only. this dress doesn't fit me. However,this dress is about $ 3183 dollars. GNU is

2013年3月8日 星期五 晴 有风



正则表达式练习



"Open Source" is a good mechanism to develop programs.


apple is my favorite food.


Football game is not use feet only.


this dress doesn't fit me.


However,this dress is about $ 3183 dollars.


GNU is free air not free beer.


Her hair is very beauty.


I can't finish the test.


Oh! The soup taste good.


motorcycle is cheap than car.


This window is clear.


the symbol '*' is represented as start.


Oh! My god!


The gd software is a library for drafting programs.


You are the best is mean you are the no. 1.


The world <Happy> is the same with "glad".


I like dog.


google is the best tools for search keyword.


goooooogle yes!


go! go! Let's go.


# I am VBird



1、过滤下载文件中包含 the 关键字


2、过滤下载文件中丌包含 the 关键字


3、过滤下载文件中丌论大小写 the 关键字


4、过滤 test 或 taste 这两个单字


5、过滤有 oo 的字节


6、过滤丌想要 oo 前面有 g 的


7、过滤 oo 前面丌想有小写字节


8、过滤有数字的那一行


9、过滤以 the 开头的


10、过滤以小写字母开头的


11、过滤开头丌是英文字母


12、过滤行尾结束为小数点.那一行


13、过滤空白行


14、过滤出 g??d 的字串


15、过滤至少两个 o 以上的字串


16、过滤 g 开头和 g 结尾但是两个 g 之间仅存在至少一个 o


17、过滤任意数字的行


18、过滤两个 o 的字串


19、过滤 g 后面接 2 到 5 个 o,然后在接一个 g 的字串


20、过滤 g 后面接 2 个以上 o 的



所给答案



[root@desktop1 ~]# grep -n 'the' regular_express.txt


[root@desktop1 ~]# grep -vn 'the' regular_express.txt


[root@desktop1 ~]# grep -in 'the' regular_express.txt


[root@desktop1 ~]# grep -n 't[ae]st' regular_express.txt


[root@desktop1 ~]# grep -n 'oo' regular_express.txt


[root@desktop1 ~]# grep -n '[^g]oo' regular_express.txt


[root@desktop1 ~]# grep -n '[^a-z]oo' regular_express.txt


[root@desktop1 ~]# grep -n '[^[:lower:]]oo' regular_express.txt


[root@desktop1 ~]# grep -n '[0-9]' regular_express.txt


[root@desktop1 ~]# grep -n '[[:digit:]]' regular_express.txt


[root@desktop1 ~]# grep -n '^the' regular_express.txt


[root@desktop1 ~]# grep -n '^[a-z]' regular_express.txt


[root@desktop1 ~]# grep -n '^[[:lower:]]' regular_express.txt


[root@desktop1 ~]# grep -n '^[^a-zA-Z]' regular_express.txt


[root@desktop1 ~]# grep -n '^[^[:alpha:]]' regular_express.txt


[root@desktop1 ~]# grep -n '.$' regular_express.txt


[root@desktop1 ~]# grep -n '^$' regular_express.txt


[root@desktop1 ~]# grep -n 'g..d' regular_express.txt


[root@desktop1 ~]# grep -n 'ooo*' regular_express.txt


[root@desktop1 ~]# grep -n 'goo*g' regular_express.txt


[root@desktop1 ~]# grep -n 'goo*g' regular_express.txt


[root@desktop1 ~]# grep -n '[0-9][0-9]*' regular_express.txt


[root@desktop1 ~]# grep -n 'o{2}' regular_express.txt


[root@desktop1 ~]# grep -n 'go{2,5}g' regular_express.txt


[root@desktop1 ~]# grep -n 'go{2,}g' regular_express.txt



练习情况



1、过滤下载文件中包含 the 关键字



[root@desktop7 Desktop]# grep -n "the" regular_express.txt --color


8:I can't finish the test.


12:the symbol '*' is represented as start.


15:You are the best is mean you are the no. 1.


16:The world <Happy> is the same with "glad".


18:google is the best tools for search keyword.


[root@desktop7 Desktop]#



2、过滤下载文件中不包含 the 关键字



[root@desktop7 Desktop]# grep -vn "the" regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


2:apple is my favorite food.


3:Football game is not use feet only.


4:this dress doesn't fit me.


5:However,this dress is about $ 3183 dollars.


6:GNU is free air not free beer.


7:Her hair is very beauty.


9:Oh! The soup taste good.


10:motorcycle is cheap than car.


11:This window is clear.


13:Oh! My god!


14:The gd software is a library for drafting programs.


17:I like dog.


19:goooooogle yes!


20:go! go! Let's go.


21:# I am VBird


22:


[root@desktop7 Desktop]#



3、过滤下载文件中不论大小写 the 关键字



[root@desktop7 Desktop]# grep -in "the" regular_express.txt --color


8:I can't finish the test.


9:Oh! The soup taste good.


12:the symbol '*' is represented as start.


14:The gd software is a library for drafting programs.


15:You are the best is mean you are the no. 1.


16:The world <Happy> is the same with "glad".


18:google is the best tools for search keyword.


[root@desktop7 Desktop]#



4、过滤 test 或 taste 这两个单字



[root@desktop7 Desktop]# grep -n "t[ea]st" regular_express.txt --color


8:I can't finish the test.


9:Oh! The soup taste good.


[root@desktop7 Desktop]#



5、过滤有 oo 的字节



[root@desktop7 Desktop]# grep -n "ooo*" regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


2:apple is my favorite food.


3:Football game is not use feet only.


9:Oh! The soup taste good.


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#



6、过滤不想要 oo 前面有 g 的



[root@desktop7 Desktop]# grep -n '[^g]oo' regular_express.txt --color


2:apple is my favorite food.


3:Football game is not use feet only.


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#



7、过滤 oo 前面不想有小写字节



[root@desktop7 Desktop]# grep -n '[^[:lower:]]oo' regular_express.txt --color


3:Football game is not use feet only.



过滤 oo 前面不想有数字



[root@desktop7 Desktop]# grep -n '[^[:digit:]]oo' regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


2:apple is my favorite food.


3:Football game is not use feet only.


9:Oh! The soup taste good.


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#



8、过滤有数字的那一行



[root@desktop7 Desktop]# grep -n "[[:digit:]]" regular_express.txt --color


5:However,this dress is about $ 3183 dollars.


15:You are the best is mean you are the no. 1.


[root@desktop7 Desktop]#



9、过滤以 the 开头的



[root@desktop7 Desktop]# grep -n "^the" regular_express.txt --color 12:the symbol '*' is represented as start.


[root@desktop7 Desktop]#



10、过滤以小写字母开头的



[root@desktop7 Desktop]# grep -n "^[[:lower:]]" regular_express.txt --color


2:apple is my favorite food.


4:this dress doesn't fit me.


10:motorcycle is cheap than car.


12:the symbol '*' is represented as start.


18:google is the best tools for search keyword.


19:goooooogle yes!


20:go! go! Let's go.


[root@desktop7 Desktop]#



11、过滤开头不是英文字母



[root@desktop7 Desktop]# grep -vn "^[[:alpha:]]" regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


21:# I am VBird


22:



检验:反向查找



[root@desktop7 Desktop]# grep -n "^[[:alpha:]]" regular_express.txt --color


2:apple is my favorite food.


3:Football game is not use feet only.


4:this dress doesn't fit me.


5:However,this dress is about $ 3183 dollars.


6:GNU is free air not free beer.


7:Her hair is very beauty.


8:I can't finish the test.


9:Oh! The soup taste good.


10:motorcycle is cheap than car.


11:This window is clear.


12:the symbol '*' is represented as start.


13:Oh! My god!


14:The gd software is a library for drafting programs.


15:You are the best is mean you are the no. 1.


16:The world <Happy> is the same with "glad".


17:I like dog.


18:google is the best tools for search keyword.


19:goooooogle yes!


20:go! go! Let's go.


[root@desktop7 Desktop]#



12、过滤行尾结束为小数点.那一行



[root@desktop7 Desktop]# dos2unix regular_express.txt


dos2unix: converting file regular_express.txt to UNIX format ...


[root@desktop7 Desktop]# grep -n '.$' regular_express.txt --color 1:"Open Source" is a good mechanism to develop programs.


2:apple is my favorite food.


3:Football game is not use feet only.


4:this dress doesn't fit me.


5:However,this dress is about $ 3183 dollars.


6:GNU is free air not free beer.


7:Her hair is very beauty.


8:I can't finish the test.


9:Oh! The soup taste good.


10:motorcycle is cheap than car.


11:This window is clear.


12:the symbol '*' is represented as start.


14:The gd software is a library for drafting programs.


15:You are the best is mean you are the no. 1.


16:The world <Happy> is the same with "glad".


17:I like dog.


18:google is the best tools for search keyword.


20:go! go! Let's go.


[root@desktop7 Desktop]#



13、过滤空白行



[root@desktop7 Desktop]# dos2unix regular_express.txt


dos2unix: converting file regular_express.txt to UNIX format ...


[root@desktop7 Desktop]# grep -n '^$' regular_express.txt --color


22:


[root@desktop7 Desktop]#



14、过滤出 g??d 的字串



[root@desktop7 Desktop]# grep -n 'g??d' regular_express.txt --color




[root@desktop7 Desktop]# grep -n 'g..d' regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


9:Oh! The soup taste good.


16:The world <Happy> is the same with "glad".


[root@desktop7 Desktop]#



15、过滤至少两个 o 以上的字串



第一种方式



[root@desktop7 Desktop]# grep -n 'ooo*' regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


2:apple is my favorite food.


3:Football game is not use feet only.


9:Oh! The soup taste good.


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#



第二种方式



[root@desktop7 Desktop]# grep -n 'o{2,}' regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


2:apple is my favorite food.


3:Football game is not use feet only.


9:Oh! The soup taste good.


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#



16、过滤 g 开头和 g 结尾但是两个 g 之间仅存在至少一个 o



[root@desktop7 Desktop]# grep -n 'goo*g' regular_express.txt --color


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#



17、过滤任意数字的行



[root@desktop7 Desktop]# grep -n '[[:digit:]]' regular_express.txt --color


5:However,this dress is about $ 3183 dollars.


15:You are the best is mean you are the no. 1.


[root@desktop7 Desktop]#



18、过滤两个 o 的字串



方法一:



[root@desktop7 Desktop]# grep -n 'oo' regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


2:apple is my favorite food.


3:Football game is not use feet only.


9:Oh! The soup taste good.


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#



方法二:



[root@desktop7 Desktop]# grep -n 'o{2}' regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


2:apple is my favorite food.


3:Football game is not use feet only.


9:Oh! The soup taste good.


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#




19、过滤 g 后面接 2 到 5 个 o,然后再接一个 g 的字串




[root@desktop7 Desktop]# grep -n 'go{2,5}g' regular_express.txt --color


18:google is the best tools for search keyword.


[root@desktop7 Desktop]#



20、过滤 g 后面接 2 个以上 o 的



方法一:



[root@desktop7 Desktop]# grep -n 'go{2,}' regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


9:Oh! The soup taste good.


18:google is the best tools for search keyword.


19:goooooogle yes!



方法二:


[root@desktop7 Desktop]# grep -n 'gooo*' regular_express.txt --color


1:"Open Source" is a good mechanism to develop programs.


9:Oh! The soup taste good.


18:google is the best tools for search keyword.


19:goooooogle yes!


[root@desktop7 Desktop]#

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读