当前位置:网站首页>Regular expression is incomplete
Regular expression is incomplete
2022-08-04 19:06:00 【sting】
准备工作.创建测试文件test.txt

一、使用grep to find a specific character
grep -n 表示显示行号
grep -i 表示不区分大小写
grep -v Indicates the opposite of the condition
grep -W 精准匹配
1、使用grep查找含有the的行
grep -n "the" test.txt (-n:表示显示行号)

2、使用grepCase-insensitive search contains the的行
grep -ni "the" test.txt (-ni 同-n -i :表示显示行号,且不区分the的大小写)

3、使用grepReverse lookup does not containthe的行
grep -n -v "the" test.txt

二、利用grep 配合[ ] to find the set of characters
^[a-z] 表示以小写字母开头
^[A-Z] 表示以大写字母开头
^[0-9]表示以数字开头
[^a-z] Indicates that so-and-so is not preceded by lowercase letters
.................................
大白话:"^"在[ ]Inside and outside have different meanings,Outside means that the content in parentheses is the beginning of the line,In it, it means that the reverse selection of the content in the brackets is the negation.
官方语言:“^”符号在元字符集合“[]”符号内外的作用是不一样的,在“[]”符号内表示反向选择,在“[]”符号外则代表定位行首.反之,若想查找以某一特定字符结尾的行则可以使用“$”定位符
1、需求:想要查找“shirt”与“short”这两个字符串时
可以发现这两个字符串均包含“sh” 与“rt”,“[]”中无论有几个字符,都仅代表一个字符,也就是说“[io]”表示匹配“i”或者“o”.
grep -n "sh[io]rt" test.txt

2、需求: Finds only single characters that contain repeats“oo”时
[[email protected] mnt]# grep -n "sh[io]rt" test.txt

3、需求: 查找“oo”前面不是“w”的字符串.使用 “[^w]”来实现
[[email protected] mnt]# grep -n '[^w]oo' test.txt

解析:会发现11行和12行,也被匹配到了.此时,是这样的,11行有d三个"ooo",And we filter for two"oo",But he identifies the latter two“oo”时两个"oo"的前面就是o而不是w.12行同理.
4、需求:若不希望“oo”前面存在小写字母,用[^a-z] 其中a-z表示小写字母,使用A-Zmeans uppercase,
[0-9]表示数字.
[[email protected] mnt]# grep -n "[^a-z]oo" test.txt

[[email protected] mnt]# grep -n "[^A-Z]oo" test.txt

[[email protected] mnt]# grep -n "[0-9]" test.txt

5、使用grep配合使用“^”(行首)表示以什么为开头、“$”(行尾)以什么为结尾
①需求:使用grepFind the beginning of the line withthe开头的.

②查询以小写字母开头的行可以通过“^[a-z]”规则来过滤,查询大写字母开头的行则使用“^[A-Z]”规则,若查询不以字母开头的行则使用“^[^a-zA-Z]”规则.
[[email protected] mnt]# grep -n '^[a-z]' test.txt

[[email protected] mnt]# grep -n '^[^a-zA-Z]' test.txt

6、grepFilter to find blank lines and dot endings
①需求:Queries with decimal points(.)结尾的行,The decimal point is a meta element in regex,具有特殊含义,用”\“,不进行转义.
[[email protected] mnt]# grep -n "\.$" test.txt

②使用grep查询空白行,"^$"
[[email protected] mnt]# grep -n "^$" test.txt

7、查找任意一个字符“.” 与重复字符“*”
在正则表达式中小数点(.)也是一个元字符,匹配任意一个字符,So you need to add the dots above to represent the end“\”
“*”stands for repeating zero or more of the preceding repeating characters,例如:“o*”表示拥有零个(即为空字符)或大于等于一个“o”的字符,因为允许空字符.
①需求:grep 查找w和dThere are two arbitrary characters in between.
[[email protected] mnt]# grep "w..d" test.txt

②”o*“表示含义0个o或者多个o

解析:表示含有0个o时,Printed all.
②”oo*“表示第一o存在,第二个o后面有个*号,表示第二个o可以是一个o或者多个o.

解析:The text contains at least oneo
③”ooo*“表示第一o存在,第二个o存在,第三个o后面有个*号,表示第三个o可以是一个o或者多个o.
[[email protected] mnt]# grep -n "ooo*" test.txt

④ 查询以 w 开头 d 结尾,中间包含至少一个 o 的字符串.

⑤ 查询以 w 开头 d 结尾,中间的字符可有可无的字符串
[[email protected] mnt]# grep -n "w.*d" test.txt

⑥查询任意数字所在行
[[email protected] mnt]# grep "[0-9][0-9]*" test.txt

8、查找连续字符范围“{}”
我们使用“.”与“*”来设定零个到无限多个重复的字符,If you want to limit a range of repeated strings,Use a range of characters from the underlying regular expression“{}”,因为“{}”在Shell中具有特殊意义,所以在使用“{}”字时,需要利用转义字符“\”,将“{}”字符转换成普通字符.
①查询两个 o 的字符
[[email protected] mnt]# grep -n "o\{2\}" test.txt

②查询以 w 开头以 d 结尾,中间包含 2~5 个 o 的字符串
[[email protected] mnt]# grep -n "wo\{2,5\}d" test.txt

③查询以 w 开头以 d 结尾,中间包含 2 以上 o 的字符串
[[email protected] mnt]# grep -n "wo\{2,\}d" test.txt

三、元字符汇总
| 元素符 | 作用(Need to express the original meaning of the symbol itself,It needs to be added before the symbol”\“) |
| ^ | "^"在[ ]Inside and outside have different meanings,Outside means that the content in parentheses is the beginning of the line,In it, it means that the reverse selection of the content in the brackets is the negation,要匹配“^” 字符本身,请使用“\^”. |
| $ | 匹配输入字符串的结尾位置,要匹配“$”字符本身,请使用“\$” |
| . | 匹配除“\r\n”之外的任何单个字符 |
| \ | 将下一个字符标记为特殊字符、原义字符、向后引用、八进制转义符.例如,‘n’匹配字符“n”. ‘\n’匹配换行符.序列‘\\’匹配“\”,而‘\(’则匹配“(” |
| * | 匹配前面的子表达式零次或多次.要匹配“*”字符,请使用“\*” |
| [] | 字符集合,匹配所包含的任意一个字符.例如,“[abc]”可以匹配“plain”中的“a” |
| [^] | Invert the contents of the square brackets. |
| [n1-n2] | 字符范围,匹配指定范围内的任意一个字符,n2>n1,范围为n1到n2Any character in the interval. |
| {} | n 是一个非负整数,匹配确定的 n 次. |
边栏推荐
猜你喜欢
随机推荐
TikTok如何为独立站引流?
从零开始实现一个简单的CycleGAN项目
mq消息积压怎么对应
EuROC 数据集格式及相关代码
VPC2187/8 电流模式 PWM 控制器 4-100VIN 超宽压启动、高度集成电源控制芯片推荐
PHP代码审计8—SSRF 漏洞
curl命令的那些事
百度智能云重庆工业互联网平台正式亮相,深耕重庆,辐射西南
《学会写作》粥佐罗著
win10 uwp ping
Redis数据库—定义、特点、安装、如何启动与停止
测试工程师如何突破职业瓶颈?
正则表达式未完
工业相机CCD与CMOS
win10 uwp win2d 使用 Path 绘制界面
SOA面向服务架构:服务、服务实例、ARXML、服务接口调用以及各参与方
Internship: changed the requirements
STP实验
如何让 JS 代码不可断点
浅谈web网站架构演变过程







