正则表达式(Regular Expression、regex或regexp,缩写为RE),也译为正规表示法、常规表示法,是一种字符模式,用于在查找过程中匹配指定的字符。
许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎,或者说Java的String也是支持正则表达式的
正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。
支持正则表达式的程序如:locate |find| vim| grep| sed |awk
元字符:那些在则表达式中具有特殊意义的专用字符,比如:点(.)、星()、问号(?)等
前导字符:即位于元字符前面的字符:abc
、aooo
.
符号 | 作用 |
---|---|
. | 匹配任意单个字符,除了换行符 |
* | 前导字符出现0次或连续多次 |
.* | 任意长度的字符 |
^ | 字符串的开头 |
$ | 字符串的结尾 |
^$ | 空字符串 |
[] | 匹配指定字符组中的任意单个字符 |
[^] | 匹配不在指定字符组内的任意字符 |
^[] | 匹配值以指定字符组中的任意一个字符开头 |
^[^] | 匹配不以指定字符组中任意一个字符开头 |
\{n\} | 匹配前导字符连续出现n次 |
\{n,\} | 匹配前导字符至少出现n次 |
\{n,m\} | 匹配前导字符出现n次与m次之间 |
\< | 取单词开头 |
\> | 取单词结尾 |
\<\> | 精确匹配字符或字符串 |
\(strings\) | 保存被匹配的字符 |
[root@localhost ~]# cat test.txt
ggle
gogle
google
gooogle
goooooogle
gooooooogle
taobao.com
taotaobaobao.com
jingdong.com
dingdingdongdong.com
10.1.1.1
Adfjd8789JHfdsdf/
a87fdjfkdLKJK
7kdjfd989KJK;
bSKJjkksdjf878.
cidufKJHJ6576,
# 包含.com
[root@localhost ~]# grep '.com' test.txt
# 以 go 开头
[root@localhost ~]# grep '^go' test.txt
# 以com结尾
[root@localhost ~]# grep 'com$' test.txt
# 匹配8和m字符
[root@localhost ~]# grep '[8m]' test.txt
# 不匹配 8 和 m 字符
[root@localhost ~] grep '[^8m]' test.txt
# 匹配以ht开头
[root@localhost ~]# grep '^[ht]' test.txt
# 匹配不以 g和t 开头
[root@localhost ~]# grep '^[^gt]' test.txt
# 精确匹配 taobao 这个字符串
[root@localhost ~]# grep '\' test.txt
# 匹配前导字符连续出现3个字符 o
[root@localhost ~]# grep 'o\{3\}' test.txt
# 匹配前导字符至少连续出现4个字符 o
[root@localhost ~]# grep 'o\{4,\}' test.txt
# 匹配前导字符o连续出现2次到4次之间
[root@localhost ~]# grep 'o\{2,4\}' test.txt
在vim模式里(通过\(strings\)
保持不变)
将192.168.44.144 变成192.167.44.168
@
和#
只是分割符(自定义的),效果一样g
:替换多次,只能替换一行%s
:针对整个文档中的所有关键词进行替换\.
:转义点符号%s/要替换的字符/替换为什么字符
\1
就等价于 192.
\2
等价于 .44
:%s#\(192\.\)168\(\.44\)\.144#\1167\2\.168
:%s@\(192\)\.168\(\.44\)\.144@\1\.167\2\.168
将光标处锁所在行10.1.1.1替换成10.1.200.200
\1
:等价于10.1
:s#\(10\.1\)\.1\.1#\1.200.200
将10.1.1.1变成10.200.200.200
g
:替换多次:%s#.1#.200#g
将helloworld yourself 换成hellolilei myself
:%s#\(hello\)world your\(self\)#\1lilei my\2#g
使用sed命令
-n
:只显示匹配结果[root@localhost ~]# sed -n 's/\(hello\)world your\(self\)/\1lilei my\2/p' test.txt
hellolilei myself
-P
可以让grep使用perl的正则表达式语法
\d
:匹配数字 [0-9]-w
: 匹配字母数字下划线[a-zA-Z0-9_]\s
:匹配空格、制表符、换页符[\t\r\n]#grep -P '\d' test.txt
#grep -P '\w' test.txt
#grep -P '\s' test.txt
使用 扩展类的正则表达式 grep -E 或则 egrep
表达式 | 作用 |
---|---|
+ | 匹配一个或多个前导字符 |
? | 匹配零个或一个前导字符 |
a |b | 匹配a或b |
() | 组字符 |
{n} | 前导字符重复n次 |
{n,} | 前导字符重复至少n次 |
{n,m} | 前导字符重复n到m次 |
表达式 | 功能 |
---|---|
[:alnum:] | 字母与数字字符 |
[:alpha:] | 字母字符(包括大小写字母) |
[:blank:] | 空格与制表符 |
[:digit:] | 数字 |
[:lower:] | 小写字母 |
[:upper:] | 大写字母 |
[:punct:] | 标点符号 |
[:space:] | 包括换行符,回车等在内的所有空白 |
创建文本文件demo.txt,输入一下内容
Aieur45869Root0000
9h847RkjfkIIIhello
rootHllow88000dfjj
8ikuioerhfhupliooking
hello world
192.168.0.254
welcome to uplooking.
abcderfkdjfkdtest
rlllA899kdfkdfj
iiiA848890ldkfjdkfj
abc
12345678908374
123456@qq.com
123456@163.com
abcdefg@itcast.com23ed
1、匹配ip地址
# grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' demo.txt
或者使用扩展正则
# grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' demo.txt
-P 可以让grep使用perl的正则表达式语法
# grep -P '(\d{1,3}\.){3}[0-9]{1,3}' demo.txt
2.、查找有数字的行(两种写法)
# grep '[0-9]' demo.txt
# grep -P '\d' demo.txt
3.、查找一个数字和一个字母连起来的
# grep -E '([0-9][a-z])|([A-Z][0-9])|([a-z][0-9])|([0-9][A-Z])' demo.txt
4、查找不以r开头的行
# grep '^[^r]' demo.txt
5、查找以数字开头的
# grep '^[0-9]' demo.txt
6、查找以大写字母开头的
# grep '^[A-Z]' demo.txt
7、查找以小写字母开头的
# grep '^[a-z]' demo.txt
8、查找以点结束的
# grep '[.]$' demo.txt
9、去掉空行
# grep ^[^$] demo.txt
10、查找完全匹配abc的行
# grep '\' demo.txt
11、查找A后有三个数字的行
# grep -E 'A[0-9]{3}' demo.txt
12、统计root在/etc/passwd里出现了几次
[root@localhost ~]# grep -o 'root' /etc/passwd > tmp.txt
[root@localhost ~]# wc -l tmp.txt
4 tmp.txt
13、用正则表达式找出自己的IP地址、广播地址、子网掩码
# ifconfig | grep -P '(\d{1,3}\.){3}[0-9]{3}'