linux 正则表达式
目录
一、正则表达式
二、元字符
三、次数符号
四、位置锚定
五、实验
一、正则表达式
通配符功能是用来处理文件名,而正则表达式是处理文本内容中字符。
分类:
1. 基本正则表达式
2.扩展正则表达式
二、元字符
元字符:
. 匹配任意单个字符,可以是一个汉字
() 使用转义符,只表示\(\)
[] 匹配指定范围内的任意单个字符,示例:[dn] [0-9] [] [a-zA-Z] [:alpha:]
[^] 匹配指定范围外的任意单个字符,示例:[^dn] [^a.z]
1.表示任意字符

2.、转义符\,表示字符本身

3.[]表示范围

4.表示除什么以外

三、次数符号
表示次数
* #匹配前面的字符任意次,包括0次,贪婪模式:尽可能长的匹配
.* #任意长度的任意字符,不包括0次,也就是匹配所有
\? #匹配其前面的字符出现0次或1次,即:可有可无
\+ #匹配其前面的字符出现最少1次,即:肯定有且 >=1 次
\{n\} #匹配前面的字符=n次
\{m,n\} #匹配前面的字符至少m次,至多n次
\{,n\} #匹配前面的字符至多n次,<=n
\{n,\} #匹配前面的字符至少n次
1.表示*所有次数0和0以上

2.任意长度

3.里面字符出现一次或0次

4.大于1次的显示

5.\{n\}显示等于多少次

6.\{m,n\}至少m次至多n次

7.\{m,\}至少m次

8.\{,n\}至多n次

四、位置锚定
^:以什么开头
$:以什么结尾
^$:指定字符
^$:空格
\b:可首可尾,必须是一个单词
-E:扩展正则表达式
1.展示以a开头的行

2.以h为结尾的内容

3.^$展示空格-v展示非空格

4.\b可以是尾部也可以是首部

5.E表示扩展正则表达式,可以不使用\

五、实验题
1.打印ip地址

2.匹配电话号码

3.匹配手机号码

4.匹配邮箱地址

5.匹配符号空格符号
