一:正则表达式的基本概念
-
正则表达式(Regular Expression, RE)是一种字符模式,用于匹配、查找、替换字符串。
-
在 Linux 中,
grep、sed、awk、vi、less等工具都支持正则。 -
分为两类:
-
基本正则表达式(BRE):
grep、sed默认使用,部分元字符需要转义。 -
扩展正则表达式(ERE):
egrep或grep -E、sed -r、awk使用,元字符更直观。
-
二:基础正则详解
搭建测试环境
cat >/gao/re.txt<<EOF
I am gao
I study linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 123456
not 123456.
my god ,i am not oldbey,but OLDBOY!
EOF
a.^ 以***开头的行
root@delu:/gao# grep '^my' re.txt
my blog is http://blog.delu.com
my qq is 123456 not 123456.
my god, i am not oldbey, but OLDBOY!
b.$ 以什么结尾的行
##查看文件以什么结尾
root@delu:/gao# cat -A re.txt
I am gao$
I study linux.$
I like badminton ball, billiard ball and chinese chess!$
my blog is http://blog.delu.com $
our size is http://book.delu.com $
$
$
my qq is 123456 not 123456.$
my god, i am not oldbey, but OLDBOY! $
root@delu:/gao# grep "com$" re.txt
root@delu:/gao# grep 'com$' re.txt
root@delu:/gao# grep 'com $' re.txt
my blog is http://blog.delu.com
our size is http://book.delu.com
root@delu:/gao# grep "com $" re.txt
my blog is http://blog.delu.com
our size is http://book.delu.com
#有空格不会去匹配 得把空格也写出来才能匹配 有几个空格 也得写
c) ^$ 空行,这行中没有任何字符,包含空格
root@delu:/gao# cat -nA re.txt
1 I am gao$
2 I study linux.$
3 I like badminton ball, billiard ball and chinese chess!$
4 my blog is http://blog.delu.com $
5 our size is http://book.delu.com $
6 $
7 $
8 my qq is 12333333456 not 12333456.$
9 123434$
10 my god, i am not oldbey, but OLDBOY! $
root@delu:/gao# grep -n '^$' re.txt
6:
7:
root@delu:/gao# grep -nv '^$' re.txt
1:I am gao
2:I study linux.
3:I like badminton ball, billiard ball and chinese chess!
4:my blog is http://blog.delu.com
5:our size is http://book.delu.com
8:my qq is 12333333456 not 12333456.
9:123434
10:my god, i am not oldbey, but OLDBOY!
d) . 任意一个字符
root@delu:/gao# grep '.' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456.
123434
my god, i am not oldbey, but OLDBOY!
e) * 连续出现,重复 a* 前一个字符连续出现0次或0次以上
root@delu:/gao# grep '3*' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456666666.
12343466678
my god, i am not oldbey, but OLDBOY!
root@delu:/gao# grep '6*' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456666666.
12343466678
my god, i am not oldbey, but OLDBOY!
f) .* 所有
###匹配所有
root@delu:/gao# grep '.*' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456666666.
12343466678
my god, i am not oldbey, but OLDBOY!
###以i开头 o结尾
root@delu:/gao# grep '^I.*o' re.txt
I am gao
I like badminton ball, billiard ball and chinese chess!
##以i开头,最后一个a结束(正则的贪婪性)
root@delu:/gao# grep '^I.*a' re.txt
I am gao
I like badminton ball, billiard ball and chinese chess!
##不写就是任意开头,c结束
root@delu:/gao# grep '^.*c' re.txt
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
正则表达式有贪婪性: 表示连续出现或所有的时候,尽可能多匹配
g) [] [abc] 一个整体,每次匹配1个字符,匹配a或b或c
root@delu:/gao# grep '[a-z]' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456666666.
my god, i am not oldbey, but OLDBOY!
root@delu:/gao# grep '[A-Z]' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my god, i am not oldbey, but OLDBOY!
root@delu:/gao# grep '[0-9]' re.txt
my qq is 12333333456 not 12333456666666.
12343466678
root@delu:/gao# grep '[a-zA-Z0-9]' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456666666.
12343466678
my god, i am not oldbey, but OLDBOY!
h) [^] [^abc] 一个整体,每次匹配1个字符,非a或非b或非c(取反)
root@delu:/gao# grep '[^abc]' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456666666.
12343466678
my god, i am not oldbey, but OLDBOY!
root@delu:/gao# grep '[^a-z]' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456666666.
12343466678
my god, i am not oldbey, but OLDBOY!
root@delu:/gao# grep '[^0-9]' re.txt
I am gao
I study linux.
I like badminton ball, billiard ball and chinese chess!
my blog is http://blog.delu.com
our size is http://book.delu.com
my qq is 12333333456 not 12333456666666.
my god, i am not oldbey, but OLDBOY!
扩展正则
|
支持命令不同
|
说明
|
|
基础正则
|
grep/sed/awk
|
|
扩展正则
|
grep -E/egrep/sed -r/awk
|
a) |或者
转义字符,脱掉马甲打回原形,去掉符号的特殊含义
b) + 连续出现,重复 前一个字符连续出现1次或1次以上
c) {} 连续出现,重复 a{n,m} 前一个字符a,连续出现至少n次,最多m次
长串拆分 6666666→ 按 {1,4}拆成 6666 + 666
d) () 被括起来的内容相当于是一个整体 ,分组与反向引用 一般用于sed命令中
root@delu:/gao# lscpu | egrep '(L2|L3)'
L2 cache: 1 MiB (1 instance)
L3 cache: 33 MiB (1 instance)
e) ? 连续出现,重复 出现1次或出现0次
- THE END -
最后修改:2026年5月26日
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:https://7d.delu7.cn/97/.html









共有 0 条评论