使用一些特殊符号+字母和数字按照某个规则组合成一个公式用来表示某个意思这就叫正则表达式^表示以什么开头
元字符
元字符:有特殊作用的字符,能描述其它字符的字符
-
基本正则
-
*表示前面的字符出现0次或者任意次
-
$表示以什么结尾
-
^表示以什么开头
-
^$代表空行
-
-
.代表单个任意字符
-
-
扩展正则 要使用egrep或者grep -E才有用
-
扩展正则支持更多的元字符,能够表示更加复杂的意思,功能更加强大
-
?表示前面的字符出现0次或者1次
-
+表示前面的字符串出现1次以上
-
| 或
-
{} 多少次
-
-
-v和[^]的区别
-
-v以行为单位
-
^以字符为单位
-
-
-
`\<` 和`\b` 表示单词以什么开头
-
`\>` 和`\b`表示单词以什么结尾
-
junjie*
junjie?
^junjie+ #^表示以junjie开头
^junjie{2,4}
wang$ #以wang结尾
#^
[root@host lianxi]# cat sc.html |egrep "^<h1"
<h1>Welcome to sanchuang!</h1>
<h1>三创同乐欢迎您!</h1>
[root@host lianxi]# cat name.txt |grep "^[^0-9]" #查找不是以数字开头的行,里面的^是取反,外面的^是以。。开头
#$
[root@host lianxi]# cat sc.html |egrep "p>$"
<p>address: hunan.changsha.furong</p>
<p>teacher feng PhoneNO: 18908495097</p>
#*
[root@host lianxi]# echo wangyuuuutaoooo |egrep "wangyu*"
wangyuuuutaoooo
[root@host lianxi]# echo wangytaoooo |egrep "wangyu*"
wangytaoooo
#{}
[root@host lianxi]# cat name.txt |egrep "shen.*"
shengjiemi lijunlin
shengjiedami shengxiaomi
[root@host lianxi]# cat name.txt |egrep "shen.{4}"
shengjiemi lijunlin
shengjiedami shengxiaomi
[root@host lianxi]# cat name.txt |egrep "shen.{4,6}" #表示shen字符串后面出现4到6个任意字符
shengjiemi lijunlin
shengjiedami shengxiaomi
#+
[root@host lianxi]# echo sannnnnnnnnchuang|egrep "san+"
sannnnnnnnnchuang
#\>
[root@host lianxi]# echo "shengjiemi shengjiemimi" |egrep "shengjiemi"
shengjiemi shengjiemimi
[root@host lianxi]# echo "shengjiemi shengjiemimi" |egrep "shengjiemi\>"
shengjiemi shengjiemimi