美文网首页
正则表达式

正则表达式

作者: 快去学习不然怎么去看aimer | 来源:发表于2019-08-05 08:57 被阅读0次

用于过滤文本时的附加条件,用来更精确的过滤文本内容。

[:alnum] 代表所以的大小写英文字符和数字 0-9 A—Z a-z
[:alpha:] 代表任意英文大小写字符  A-Z a-z
[:lower:] 代表小写字符       a-z
[:upper:] 代表大写字符        A-Z
[:digit:] 代表数字             0-9

wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt 获取文本

开始试验

grep -n "the" regular_express.txt  打印包含the的行,并打印行号
grep -in "the" regular_express.txt  打印包含the的行,并打印行号,并忽略大小写
grep -inv "the" regular_express.txt 打印不包含the的行,并打印行号,并忽略大小写

正则

-bash-4.2# grep -n "t[ea]st" regular_express.txt   #[]括号里的‘e,a’有且只有一个
8:I can't finish the test.
9:Oh! The soup taste good.
-bash-4.2# grep -n "[^g]oo" regular_express.txt    #查询不已g为开头且包含oo的行
2:apple is my favorite food.
3:Football game is not use feet only.
19:google is the best tools for search keyword.
20:goooooogle yes!
21:goooooooood!
23:gooogle yesssssssss!
相对应的
-bash-4.2# grep -n "^[g]oo" regular_express.txt    #查询以goo为开头行
19:google is the best tools for search keyword.
20:goooooogle yes!
21:goooooooood!
22:google 12!
23:gooogle yesssssssss!
[root@rourou ~]# grep -n '[0-9]' regular_express.txt 
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.         找包含0-9的行
并且
[root@rourou ~]# grep -n '[0-9][0-9]*' regular_express.txt 
5:However, this dress is about $ 3183 dollars.           找包含0-9的行,后一个0-9后面加了*所以后一
15:You are the best is mean you are the no. 1.          个0-9可以重复0到n次
同样的
[root@rourou ~]# grep -n '[0-9][0-9][0-9]*' regular_express.txt 
5:However, this dress is about $ 3183 dollars.        与上一题一样,不过必须是两位数及两位数以上的
[root@rourou ~]# grep -n '^$' regular_express.txt 
25:           以换行符为开头,所以查到的为空白行
[root@rourou ~]# grep -n 'g..d' regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.                         查询g与d中间间隔为2的行       
16:The world <Happy> is the same with "glad".       
同样的
[root@rourou ~]# grep -n 'g...d' regular_express.txt 
26:goood
27:gwewd        查询g与d中间间隔为3的行 
grep -n 'go\{2,5\}g' regular_express.txt    筛选g与g之间有2到5个o,\的作用在于将特殊字符的特殊含 
                                                               义去掉.o为判定字符,因此第一个o不计算

grep扩展

-An: 查找行的后几行
-Bn: 查找行的前几行

[root@rourou ~]# grep mail /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
所以:
[root@rourou ~]# grep mail -A2 -B3 /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
[root@rourou ~]# grep -o mail /etc/passwd            -o  只输出查找项
mail
mail
mail
[root@rourou ~]# grep -o -c 'mail' /etc/passwd  -c  统计数量 
1
[root@rourou ~]# grep -l mail /etc/passwd
/etc/passwd                                                   -l  得到文件路径
grep -r root /etc    -r 在/etc下查找包含root的行

扩展正则

[root@rourou ~]# egrep -n 'go+d' regular_express.txt    包含1到n个o的行 
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
13:Oh! My god!
[root@rourou ~]# egrep -n 'go?d' regular_express.txt   包含0个或1个o
13:Oh! My god!
14:The gd software is a library for drafting programs.
[root@rourou ~]# egrep -n 'go|goo' regular_express.txt               go与goo是或的关系
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
13:Oh! My god!
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
[root@rourou ~]# egrep -n 'g(o|oo)d' regular_express.txt           g与d是固定的,o与oo是或的关系
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
13:Oh! My god!
[root@rourou ~]# echo 'AxyzxyzxyzC' | egrep 'A(xyz)+C'    重复部分用单个的代替
AxyzxyzxyzC
前面的即使不是连续的也是可以的
[root@rourou ~]# echo 'zxyzxyzzxyyxzy' | egrep [xyz]+
zxyzxyzzxyyxzy

相关文章

  • Linux命令行与Shell脚本编程大全-shell正则表达式

    本章内容: 定义正则表达式 了解基本正则表达式 扩展正则表达式 创建正则表达式 定义正则表达式 正则表达式是你定义...

  • 正则相关

    正则表达式基本语法 正则表达式常见字符 正则表达式特殊字符 正则表达式数量词 正则表达式边界匹配 正则表达式逻辑或...

  • 正则表达式系列-1

    正则表达式系列-1正则表达式系列-2正则表达式系列-3正则表达式系列-4 什么是正则表达式 正则表达式就是用事先定...

  • 正则表达式

    正则表达式 - 教程正则表达式 - 简介正则表达式 - 语法正则表达式 - 元字符正则表达式 - 运算符优先级正则...

  • Python基础入门 - 正则表达式与综合实战

    1. 初识正则表达式 1.1 介绍 步骤介绍正则表达式入门及应用正则表达式的进阶正则表达式案例 1.2 正则表达式...

  • Java正则表达式参考

    Java正则表达式入门 java正则表达式应用 深入浅出之正则表达式(一) 深入浅出之正则表达式(二) 正则表达式...

  • 正则表达式

    正则表达式 正则表达式就是记录文本规则的代码 正则表达式常用的元字符 正则表达式常用的限定符 正则表达式举例:这里...

  • Python爬虫(十)_正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • python正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • 正则表达式

    了解正则表达式基本语法 能够使用JavaScript的正则对象 正则表达式简介 什么是正则表达式 正则表达式:用于...

网友评论

      本文标题:正则表达式

      本文链接:https://www.haomeiwen.com/subject/epnjdctx.html