第二十四天笔记

第二十四天笔记

for 和 select 循环语句的应用实践

Linux 系统产生随机数的方法

bash 复制代码
[han@han-shell ~]$ echo "han$RANDOM"|md5sum |cut -c 1-8
884075a2

Linux 正则表达式

环境准备

bash 复制代码
[han@shell ~]$ vim words
cat
category
acat
concatenate
dog

普通字符

bash 复制代码
[han@shell ~]$ cat words |grep 'cat'
cat
category
acat
concatenate

字符集

[...]

匹配 [...] 中的任意一个字符。

bash 复制代码
[han@shell ~]$ echo cbt >> words
[han@shell ~]$ echo c1t >> words
[han@shell ~]$ cat words | grep 'c[ab]t'
cat
category
acat
concatenate
cbt
[a-z] [A-Z] [0-9]
bash 复制代码
[han@shell ~]$ cat words | grep 'c[a-z]t'
cat
category
acat
concatenate
cbt
[han@shell ~]$ echo cCt >> words
[han@shell ~]$ cat words | grep 'c[A-Z]t'
cCt
[han@shell ~]$ cat words | grep 'c[0-9]t'
c1t
[han@shell ~]$ cat words | grep 'c[a-z0-9]t'
cat
category
acat
concatenate
cbt
c1t
[han@shell ~]$ cat words | grep 'c[a-zA-Z0-9]t'
cat
category
acat
concatenate
cbt
c1t
cCt
# 要想匹配-符号,将改符号写在第一个位置
[han@shell ~]$ echo c-t >> words
[han@shell ~]$ cat words | grep 'c[-a-zA-Z0-9]t'
cat
category
acat
concatenate
cbt
c1t
cCt
c-t
[^...]

匹配除了 [...] 中字符的所有字符。

bash 复制代码
[han@shell ~]$ cat words | grep 'c[^ab]t'
c1t
# ^放中间会被当做普通字符
[han@shell ~]$ cat words | grep 'c[a^b]t'
cat
category
acat
concatenate
cbt
.

匹配除换行符( \n 、 \r )之外的任何单个字符,相等于 [^\n\r]

bash 复制代码
[han@shell ~]$ cat words | grep 'c.t'
cat
category
acat
concatenate
cbt
c1t
cCt
c-t
\

将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。

bash 复制代码
[han@shell ~]$ echo c.t >> words
[han@shell ~]$ cat words | grep 'c\.t'
c.t
# 匹配普通字符,虽然可以匹配,但强烈建议不要在前面加\
[han@shell ~]$ cat words | grep 'c\at'
cat
category
acat
concatenate
|

| 符号是扩展表达式中元字符,指明两项之间的一个选择。要匹配 | ,请使用 |

bash 复制代码
# 使用egrep或者grep -E 匹配
[han@shell ~]$ cat words | egrep 'cat|dog'
# 或者
[han@shell ~]$ cat words | grep -E 'cat|dog'
cat
category
acat
concatenate
dog

定位符

^

匹配行首位置。

bash 复制代码
[han@shell ~]$ cat words | grep '^cat'
cat
category
$

匹配行末位置。

bash 复制代码
[han@shell ~]$ cat words | grep 'cat$'
cat
acat
[han@shell ~]$ cat words | grep '^cat$'
cat
\b

匹配一个单词边界

bash 复制代码
[han@shell ~]$ echo hello cat >> words
[han@shell ~]$ cat words | grep '\bcat'
cat
category
hello cat
[han@shell ~]$ cat words | grep 'cat\b'
cat
acat
hello cat
[han@shell ~]$ cat words | grep '\bcat\b'
cat
hello cat
\B

非单词边界匹配

bash 复制代码
[han@shell ~]$ cat words | grep '\Bcat'
acat
concatenate

限定次数

*

匹配前面的子表达式任意次数

bash 复制代码
[han@shell ~]$ echo dg >> words
[han@shell ~]$ echo doog >> words
[han@shell ~]$ cat words | grep 'do*g'
dog
dg
doog
?

? 是扩展表达式元字符,匹配前面的子表达式一次以下次数

bash 复制代码
[han@shell ~]$ cat words | egrep 'do?g'
dog
dg
{n}

{} 是扩展表达式元字符,用于匹配特定次数。例如: {n} ,配置n次。

bash 复制代码
[han@shell ~]$ cat words | egrep 'do{2}g'
doog
{m,n}

{m,n} ,是扩展表达式元字符,用于匹配次数介于m-n之间。

bash 复制代码
[han@shell ~]$ echo dooog >> words
[han@shell ~]$ echo doooog >> words
[han@shell ~]$ cat words | egrep 'do{2,3}g'
doog
dooog
{m,}

{m,} ,是扩展表达式元字符,匹配前面的子表达式m次以上次数。

bash 复制代码
[han@shell ~]$ cat words | egrep 'do{2,}g'
doog
dooog
doooog
{,n}

{,n} ,是扩展表达式元字符,匹配前面的子表达式n次以下次数

bash 复制代码
[han@shell ~]$ cat words | egrep 'do{,3}g'
dog
doog
dg
dooog
**思考: **

如何过滤出以下文件中所有有效IPv4地址?

bash 复制代码
'\b(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]|[1-9][0-
9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-
4][0-9]|25[0-5])\b'
相关推荐
智者知已应修善业7 分钟前
【51单片机4个IO实现16按键可扩展独立按键64矩阵驱动显示矩阵原值】2023-5-8
c++·经验分享·笔记·算法·51单片机
深蓝海拓9 分钟前
S7-1500学习笔记:Array数据类型
笔记·学习·plc
Xudde.8 小时前
班级作业笔记报告0x04
笔记·学习·安全·web安全·php
zzh08111 小时前
MySQL高可用集群笔记
数据库·笔记·mysql
绛橘色的日落(。・∀・)ノ11 小时前
Matplotlib实践学习笔记
笔记·学习
chase。11 小时前
【学习笔记】AGILE:把人形机器人强化学习从“玄学”变成“工程学”
笔记·学习·敏捷流程
久菜盒子工作室11 小时前
高等教育学|第一章高等教育概述
经验分享·笔记·课程设计
tq108612 小时前
语言流形与思维共生:中西认知图景的差异与交融
笔记
YuanDaima204812 小时前
基于 LangChain 1.0 的检索增强生成(RAG)实战
人工智能·笔记·python·langchain·个人开发·langgraph
_李小白13 小时前
【OSG学习笔记】Day 31: 渲染到纹理(RTT)
笔记·数码相机·学习