正则练习题
-
正则含义
-
1.1 基础正则
^ 以什么开头,"^yuchao" 表示匹配以yuchao单词开头的行 $ 以什么结尾,"yuchao$",表示匹配以yuchao单词结尾的行 ^$ 组合符号,表示空行。逻辑解释就是以^开始,以$结尾的行 . 匹配任意且只有一个字符,但是不匹配空行 \ 转义字符,让特殊符号失效,如"\."只表示小数点 * 匹配前一个字符(连续出现)0次或1次以上,注意,当重复0次的时候,表示什么也没有,但是会撇皮所有内容 .* 组合符,匹配所有内容 [abc] 匹配[]内包含的任意一个字符a或b或c [a-z] 匹配[]内包含a-z任意一个字符 [0-9] 匹配[]内包含0-9的任意一个数字 [^abc] 匹配不包含^后的任意字符a或b或c,这里的^表示对[abc]的取反,与在外面的^意义不同
-
1.2 扩展正则
+ 匹配前1个字符1次或多次 [:/]+ 匹配括号内的 ":" 或 "/" 字符1次或多次 ? 匹配前一个字符0次或1次 | 表示或者,即同时过滤多个字符串 () 分组过滤,被括起来的内容表示一个整体,另外()的内容可以被后面的\n引用,n为数字,表示引用第几个括号的内容 \n 引用前面()里的内容,例如(abc)\1 表示匹配abcabc a{n,m} 匹配前一个字符最少n次,最多m次 a{n,} 匹配前一个字符最少n次 a{,m} 匹配前一个字符最多m次 a{n} 匹配前一个字符正好n次
注:基础正则也可以使用扩展正则的符号,只是需要加
\
-
-
基础正则练习题
-
2.1 测试数据
➜ ~ vim bre.txt I am teacher yuchao. I teach linux,python. testtttsss000123566666I like video,games,girls #I like girls. $my blog is http://yuchaoit.cn/ #my site is https://www.cnblogs.com/pyyu My qq num is 877348180. my phone num is 15210888008.
-
2.2 查找
I
开头的行➜ ~ grep '^I' bre.txt I am teacher yuchao. I teach linux,python. I like video,games,girls
-
2.3 查找以任意字符结尾的行
# 1. 查找以u结尾的行 ➜ ~ grep 'u$' bre.txt #my site is https://www.cnblogs.com/pyyu# 2. 查找以 . 结尾的行 ➜ ~ grep '\.$' bre.txt I am teacher yuchao. I teach linux,python. #I like girls. My qq num is 877348180. my phone num is 15210888008.
-
2.4 排除注释行和空行
# 1. 使用扩展正则 ➜ ~ grep -Ev '^$|^#' bre.txt I am teacher yuchao. I teach linux,python. testtttsss000123566666 I like video,games,girls $my blog is http://yuchaoit.cn/ My qq num is 877348180. my phone num is 15210888008.# 2. 使用基本正则 ➜ ~ grep -v '^$\|^#' bre.txt I am teacher yuchao. I teach linux,python. testtttsss000123566666 I like video,games,girls $my blog is http://yuchaoit.cn/ My qq num is 877348180. my phone num is 15210888008.# 3. 找出以I开头,s结尾的行 ➜ ~ grep -E '^I.*s$' bre.txt I like video,games,girls
-
2.5 匹配除空行以外的所有字符(包含空格)
➜ ~ grep -n '.' bre.txt 1:I am teacher yuchao. 2:I teach linux,python. 3:testtttsss000123566666 5:I like video,games,girls 6:#I like girls. 7:$my blog is http://yuchaoit.cn/ 8:#my site is https://www.cnblogs.com/pyyu 9:My qq num is 877348180. 10:my phone num is 15210888008.
注:
.
不匹配空行及\n
-
2.6 找出以
$
开头的行# 方法1: ➜ ~ grep '^\$' bre.txt $my blog is http://yuchaoit.cn/# 方法2: ➜ ~ grep '[$].*' bre.txt $my blog is http://yuchaoit.cn/
-
2.7 匹配字符
# 1. 匹配小写字母的单词
# 2. 找出以 . 或者s结尾的行 ➜ ~ grep -E '\.$|s$' bre.txt I am teacher yuchao. I teach linux,python. I like video,games,girls #I like girls. My qq num is 877348180. my phone num is 15210888008.# 3. 找出以I开头,且结尾不是.的行 ➜ ~ grep -E '^I.*[^.]$' bre.txt I like video,games,girls
-
-
扩展正则练习题
-
3.1 测试数据
➜ ~ cat ere.txt I am teacher yuchao. I teach linux,python. testtttsss000123566666I like video,games,girls #I like girls. $my blog is http://www.yuchaoit.cn #my site is https://www.cnblogs.com/pyyu My qq num is 877348180. my phone num is 15210888008.
-
3.2 匹配手机号
# 方法1: ➜ ~ grep -Eo '\b[1][3456789][0-9]{9}\b' ere.txt 15210888008# 方法2: ➜ ~ grep -o '\b[0-9]\{11\}\b' ere.txt 15210888008
-
3.3 匹配QQ号
➜ ~ grep -Eo '\b[0-9]{5,9}\b' ere.txt 877348180# QQ号码的位数通常为5-9位
-
3.4 查找出所有单词中出现字母连续的行(比如:
www
,http
)➜ ~ grep -Eo '([a-z])\1' ere.txt -n 3:tt 3:tt 3:ss 7:tt 7:ww 8:tt 8:ww 8:yy 9:qq➜ ~ grep -E '([a-z])\1' ere.txt -n 3:testtttsss000123566666 7:$my blog is http://www.yuchaoit.cn 8:#my site is https://www.cnblogs.com/pyyu 9:My qq num is 877348180.
-
3.5 只查找出同一个字母连续3次的行,比如
www
➜ ~ grep -Eo '([[:alpha:]])\1{2}' ere.txt ttt sss www www
-
3.6 提取网址
# 方法1: ➜ ~ grep -E '(www).*(com|cn)' ere.txt -o www.yuchaoit.cn www.cnblogs.com# 方法2:限制二级域名只包括字母数字不含特殊字符(根据文件内容进行正则定制化配置) ➜ ~ grep -E '([[:alpha:]])\1{2}\.[[:alnum:]]+\.[[:alpha:]]{2,3}' ere.txt -o www.yuchaoit.cn www.cnblogs.com
-
3.7 提取完整网址
# 方法1:提取域名不包括uri信息 ➜ ~ grep -E '[a-z]{4,5}:[/]{2}([[:alpha:]]+\.){2}[[:alpha:]]{2,3}' ere.txt -o http://www.yuchaoit.cn https://www.cnblogs.com# 方法2:提取域名的URL及URI信息 ➜ ~ grep -E '[a-z]{4,5}:[/]{2}([[:alpha:]]+\.){2}[[:alpha:]]{2,3}[/]?.*' ere.txt -o http://www.yuchaoit.cn https://www.cnblogs.com/pyyu# 方法3:简便写法 ➜ ~ grep -E 'http[s]?.*' ere.txt -o http://www.yuchaoit.cn https://www.cnblogs.com/pyyu
-
-
实战练习
-
4.1 排除配置文件的注释、空行
# 方法1:推荐使用 ➜ ~ grep -Env '^#|^$' ere.txt 1:I am teacher yuchao. 2:I teach linux,python. 3:testtttsss000123566666 5:I like video,games,girls 7:$my blog is http://www.yuchaoit.cn 9:My qq num is 877348180. 10:my phone num is 15210888008.#方法2: ➜ ~ grep -nv -e '^$' -e '^#' ere.txt 1:I am teacher yuchao. 2:I teach linux,python. 3:testtttsss000123566666 5:I like video,games,girls 7:$my blog is http://www.yuchaoit.cn 9:My qq num is 877348180. 10:my phone num is 15210888008.# 方法3:(注意:此处的行号并非原文件行号,是匹配后重新排序的行号) ➜ ~ grep -v '^#' ere.txt | grep -nv '^$' 1:I am teacher yuchao. 2:I teach linux,python. 3:testtttsss000123566666 5:I like video,games,girls 6:$my blog is http://www.yuchaoit.cn 7:My qq num is 877348180. 8:my phone num is 15210888008.
-
4.2 查找某进程是否存在,过滤
grep
临时进程➜ ~ ps -ef | grep 'sshd' | grep -v 'grep' root 1011 1 0 Apr09 ? 00:00:00 /usr/sbin/sshd -D root 6505 1011 0 Apr11 ? 00:00:17 sshd: root@pts/0,pts/1
-
4.3 查看sda磁盘的使用率
# 方法1: ➜ ~ df -h /dev/sda | grep -Eo '[[:digit:]]{1,3}%' 0%# 方法2: ➜ ~ df /dev/sda | grep -Eo '[0-9]+%' 0%
-
4.4 查看根分区的磁盘使用率
# 方法1: ➜ ~ df / | grep -Eo '[[:digit:]]+%' 22%# 方法2: ➜ ~ df | grep '/$' | grep -Eo '[0-9]{1,3}%' 22%
-
4.5 取出IP地址
# 方法1: ➜ ~ ifconfig ens33 | grep 'inet\b' | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -1 10.0.0.10# 方法2: ➜ ~ ifconfig ens33 | grep -E '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' -o | head -1 10.0.0.10# 方法3: ➜ ~ ifconfig ens33 |grep 'inet\b' | grep -E '([0-9]{1,3}[\.]?){4}' -o | head -1 10.0.0.10# 方法4: ➜ ~ ifconfig ens33 | grep 'inet\b' | grep -E '([[:digit:]]{1,3}[\.]?){4}' -o | head -1 10.0.0.10# 方法5: ➜ ~ ip address show ens33 | grep 'inet\b' | grep -E '([0-9]+[.]?){4}' -o | head -1 10.0.0.10
-
4.4 统计出系统中所有禁止登录的用户且只显示用户名
# 方法1: ➜ ~ grep 'nologin' /etc/passwd | grep -Eo '^[[:alnum:]]+'# 方法2: ➜ ~ grep 'nologin' /etc/passwd| grep -Eo '^[a-zA-Z0-9]{1,}' # 方法3: ➜ ~ sed -nr '/nologin/s#^([[:alnum:]]+):.*#\1#p' /etc/passwd# 方法4: ➜ ~ grep 'nologin' /etc/passwd | sed -nr 's#^([[:alnum:]]+):.*#\1#p'
-
4.5 找出由root创建的用户
# 方法1: ➜ ~ grep -E '[0-9]{4,}' /etc/passwd# 方法2: ➜ ~ grep -E '[[:digit:]]{4,}' /etc/passwd# 方法3: ➜ ~ sed -nr 's/^([[:alnum:]]+):.*([0-9]{4,}):.*/user:\1 id:\2 /p' /etc/passwd user:jack id:1000 user:andy id:1004 user:mike id:1005 user:jack01 id:1006 user:pyyu id:1007
-