正则表达式基本知识.docx
- 文档编号:29239871
- 上传时间:2023-07-21
- 格式:DOCX
- 页数:8
- 大小:16.19KB
正则表达式基本知识.docx
《正则表达式基本知识.docx》由会员分享,可在线阅读,更多相关《正则表达式基本知识.docx(8页珍藏版)》请在冰豆网上搜索。
正则表达式基本知识
正则表达式:
什么是正则表达式?
正则表达式是一种字符模式,用一组特殊的元字符匹配我们想要的内容
也叫正规表达式或规则表达式。
操作的对象是文本文件,目的是加快处理文本文件的速度。
使用的时候一定要注意使用正确的元字符,否则会得到错误的结果。
如何学习正则表达式?
从例子开始,理解例子、修改例子、写出例子
注意事项:
1,你要熟悉文件的结构
2,正则表达式不是万能的
介绍元字符:
(1).匹配除换行符之外的任意的单个字符。
\n换行符
grep使用grep命令测试元字符的应用
[root@teachertest]#grep'.sh'passwd
[root@teachertest]#grep--color'.sh'passwd
(2)$行尾定位符
[root@teachertest]#grep--color'sh$'passwd
(3)^行首定位符
[root@teachertest]#grep--color'root'passwd
[root@teachertest]#grep--color'^root'passwd
练习:
1,第2个字符是o的行
[root@teachertest]#grep--color'^.o'passwd
第三个字符是o的行
[root@teachertest]#grep--color'^..o'passwd
2,找出用户名是5个字符的行
[root@teachertest]#grep--color'^.....:
x'passwd
3,找出用户名是5个字母的行
[root@teachertest]#grep--color'^[a-Z][a-Z][a-Z][a-Z][a-Z]:
'passwd
(4)[]匹配指定字符组内的任意一个字符
["abc"]==abc"
支持“-”:
[a-z]所有的小写字母
[A-Z]所有的大写字母
[a-Z]所有的字母
[0-9]所有的数字
[^...]取反
[^a-z]所有的非小写字母
[^a-Z0-9]所有的符号
(5)\{m\}:
前面的条件出现m次
\{m,\}:
前面的条件出现>=m次
\{m,n\}:
前面的条件出现m到n次
m和n的范围是0-255之间的整数
[root@teachertest]#grep--color'^[a-Z]\{5\}:
'passwd
[0-9][a-Z]\{5\}#1个数字,5个字母
(6)*匹配前面的字符有0个或任意多个
[root@teachertest]#cat123
rt
rrt
rot
aot
bot
[root@teachertest]#grep--color'r*'123
[root@teachertest]#grep--color'rr*'123
.*通吃
练习:
找出uid是3位数的行
[root@teachertest]#grep--color'^.*:
x:
[0-9]\{3\}:
'passwd
[root@teachertest]#grep--color'x:
[0-9]\{3\}:
'passwd
找出一行里面包含2个root的行
grep'root.*root'passwd
(7)\(...\)标签,保存被匹配的字符,最多能用9个,从左到右,123
[root@teachertest]#grep--color'\(root\).*\1'passwd
root:
x:
0:
0:
root:
/root:
/bin/bash
[root@teachertest]#grep--color'\(root\).*\1.*\1'passwd
root:
x:
0:
0:
root:
/root:
/bin/bash
(8)\转义字符,屏蔽元字符的特殊含义
[root@teachertest]#grep--color'r.t'123
r.t
r*t
r^t
r$t
rot
[root@teachertest]#grep--color'r\.t'123
r.t
(9)\<词首定位符
\>词尾定位符词:
单词
以上元字符都是正则表达式最基本的元字符
*[:
alnum:
]字母+数字
*[:
alpha:
]字母
[:
cntrl:
]控制字符
*[:
digit:
]十进制数
[:
graph:
]非空字符
*[:
lower:
]小写字母
*[:
upper:
]大写字母
[:
print:
]非空字符
*[:
punct:
]符号
*[:
space:
]空白字符(空格和tap)
[:
xdigit:
]十六进制数
上述命令使用时用[]包上使用
例如:
[[:
alpha:
]]
[[:
alpha]]
[^[:
alpha:
]]取反向的
grep命令的选项:
-n显示行号
-v取反
-r递归搜索
-i不区分大小写
[root@teachertest]#grep-n'root'passwd
[root@teachertest]#grep-v'root'passwd
[root@teachertest]#grep-r'root'/test/
[root@teachertest]#grep-i'root'passwd
-c统计匹配的行数
[root@teachertest]#grep-c'oo'passwd
-num显示匹配行周围的num行
-Anum显示匹配行下面的num行
-Bnum显示匹配行上面的num行
[root@teachertest]#grep32123
32
[root@teachertest]#grep-1'32'123
wqwe
32
we
[root@teachertest]#grep-2'32'123
123
wqwe
32
we
1
[root@teachertest]#grep-A2'32'123
32
we
1
[root@teachertest]#grep-B2'32'123
123
wqwe
32
-----------------------------
扩展正则的元字符:
egrep==grep-E
^
$
.
*上面四个元字符跟基本正则表达式使用方法一样
{m}匹配前面的字符有m个
{m,}=\{m,\}
{m,n}=\{m,n\}
(root)标签=\(root\)
?
前面的字符有0个或1个
+前面的字符有1个或多个
|或
[root@teacherbj]#egrep--color'2\.?
[0-9]'datafile
等同于下列两条命令
[root@teacherbj]#grep-E--color'2\.?
[0-9]'datafile
[root@teacherbj]#grep--color'2\.\?
[0-9]'datafile
[root@teacherbj]#egrep--color'3+'datafile
等同于下个命令
[root@teacherbj]#grep--color'33*'datafile
[root@teacherbj]#egrep'NW|WE'datafile
[root@teacherbj]#egrep'^(no|we)'datafile
等同于下个命令
[root@teacherbj]#egrep'^no|^we'datafile
fgrep:
匹配固定字符串
[root@teachertest]#cat321
a**********
b..........
[root@teachertest]#grep'a\*'321
a**********
[root@teachertest]#fgrep'a*'321
a**********
[root@teachertest]#fgrep'b.'321
b..........
练习题:
1,样例文件是datafile
(1)找出含有NW的行
grepNW
(2)找出以n或s开头的行
egrep'^(n|s)'
grep'^[ns]'
(3)找出以3结尾的行
3$
(4)找出含有.5的行
\.5
(5)找出开头是north的行
grep'^north'
(6)找出含有2个大写字母一个空白符
一个大写字母的行
[A-Z]\{2\}[[:
space:
]][A-Z]
(7)找出含有连续的9个小写字母的行
[[:
lower:
]]\{9\}
[a-z]\{9\}
[a-z]{9}
2,
(1)匹配所有的QQ号[1-9][0-9]\{4,\}
(2)匹配所有的邮箱dfsd123_@sdfh123.asdfdfjkl.asdfs
[[:
alnum:
]_]+@[[:
alnum:
]]+\.[[:
alpha:
]]+(\.[[:
alpha:
]])*
(3)匹配身份证号
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 正则 表达式 基本知识
