Linux 的通配符与正则表达式

柳三千

文章最后更新时间:2025年01月22日

通配符与正则表达式区别与联系:

1. 区别:


    通配符

        主要用于文件名匹配,由 shell 解释,用于 lscpmvrmfind 等命令。

        功能相对简单,侧重于文件名的匹配,例如文件扩展和部分字符的匹配。

    正则表达式

        更强大和灵活,用于文本处理,由文本处理工具(如 grepsedawk)解释。

        可用于复杂的文本匹配、查找、替换和提取操作,可处理行内的字符模式。

2. 联系:


    它们都使用一些相似的符号(如 *?[]),但含义可能不同。

    都是为了匹配或筛选字符序列,但应用场景和解释器不同。


基本通配符

 *(星号):代表零个或多个字符。

     ls *.txt 会列出所有以 .txt 结尾的文件,包括 file1.txtfile2.txtreport.txt 等。

     rm *bak 会删除所有以 bak 结尾的文件。    

例如:

[ldx@VM-20-5-opencloudos ~]$ ls
1.txt  2.txt  alss  remoce.txt  test
[ldx@VM-20-5-opencloudos ~]$ ls *.txt
1.txt  2.txt  remoce.txt
[ldx@VM-20-5-opencloudos ~]$ touch 1.bak w.bak lll.bak
[ldx@VM-20-5-opencloudos ~]$ ls
1.bak  1.txt  2.txt  alss  lll.bak  remoce.txt  test  w.bak
[ldx@VM-20-5-opencloudos ~]$ rm -f *.bak
[ldx@VM-20-5-opencloudos ~]$ ls
1.txt  2.txt  alss  remoce.txt  test


?(问号):代表一个字符。

    ls file?.txt 会列出 file1.txtfile2.txt 等,但不会列出 file10.txt,因为 ? 只代表一个字符。

例如:

[ldx@VM-20-5-opencloudos ~]$ ls
1.txt  2.txt  alss  remoce.txt  test
[ldx@VM-20-5-opencloudos ~]$ ls ?.txt
1.txt  2.txt

   

[](中括号):代表一个字符的范围或集合。

    ls file[1-3].txt 会列出 file1.txtfile2.txt 和 file3.txt

    ls file[a-z].txt 会列出以 file 开头,后面跟一个小写字母,再跟 .txt 的文件。

例如:

[ldx@VM-20-5-opencloudos ~]$ ls
1.txt  2.txt  3.txt  4.txt  5.txt  alss  remoce.txt  test
[ldx@VM-20-5-opencloudos ~]$ ls [1-3].txt
1.txt  2.txt  3.txt
[ldx@VM-20-5-opencloudos ~]$ ls
1a.txt  1.txt  2.txt  3.txt  4.txt  5.txt  ab.txt  alss  remoce.txt  test  x.txt
[ldx@VM-20-5-opencloudos ~]$ ls [a-z].txt
x.txt
[ldx@VM-20-5-opencloudos ~]$ ls a[a-z].txt
ab.txt
[ldx@VM-20-5-opencloudos ~]$ ls r[a-z].txt
ls: cannot access 'r[a-z].txt': No such file or directory
[ldx@VM-20-5-opencloudos ~]$ ls [a-z]*.txt
ab.txt  remoce.txt  x.txt


[^ ] 或 [! ](中括号内的 ^ 或 !):代表不在范围或集合内的字符。

    s file[^a-z].txt 会列出以 file 开头,后面跟一个非小写字母,再跟 .txt 的文件。

例如:

[ldx@VM-20-5-opencloudos ~]$ ls [a-z]*.txt
ab.txt  remoce.txt  x.txt
[ldx@VM-20-5-opencloudos ~]$ ls [!a-z]*.txt
1a.txt  1.txt  2.txt  3.txt  4.txt  5.txt
[ldx@VM-20-5-opencloudos ~]$ ls [^a-z]*.txt
1a.txt  1.txt  2.txt  3.txt  4.txt  5.txt
[ldx@VM-20-5-opencloudos ~]$ touch ABC.txt
[ldx@VM-20-5-opencloudos ~]$ ls [^a-z]*.txt
1a.txt  1.txt  2.txt  3.txt  4.txt  5.txt  ABC.txt
[ldx@VM-20-5-opencloudos ~]$


正则表达式

1. 基本元字符:


.(点):代表任意一个字符。

    例如,a.c 可以匹配 abcaacaxc 等。


^(脱字符):匹配行的开始。

    例如,^start 会匹配以 start 开头的行。


$(美元符号):匹配行的结束。

    例如,end$ 会匹配以 end 结尾的行。


*(星号):匹配前面的元素零次或多次。

    例如,ab*c 可以匹配 acabcabbcabbbc 等。


+(加号):匹配前面的元素一次或多次。

    例如,ab+c 可以匹配 abcabbcabbbc 等,但不匹配 ac


?(问号):匹配前面的元素零次或一次。

    例如,ab?c 可以匹配 ac 或 abc


{n}匹配前面的元素恰好 n 次。

    例如,ab{2}c 会匹配 abbc


{n,}匹配前面的元素至少 n 次。

    例如,ab{2,}c 会匹配 abbcabbbc 等。


{n,m}匹配前面的元素 n 到 m 次。

    例如,ab{2,4}c 会匹配 abbcabbbcabbbbc

[](中括号):匹配括号内的任意一个字符。

    例如,[abc] 会匹配 ab 或 c


[^ ] 或 [! ](中括号内的 ^ 或 !):匹配不在括号内的任意一个字符。

    例如,[^abc] 会匹配除 abc 外的任何字符。


2. 特殊字符类:


\d 或 [0-9]匹配一个数字。

\D 或 [^0-9]匹配一个非数字字符。

\w 或 [a-zA-Z0-9_]匹配一个单词字符(字母、数字或下划线)。

\W 或 [^a-zA-Z0-9_]匹配一个非单词字符。
\s匹配一个空白字符(空格、制表符、换行符等)。

\S匹配一个非空白字符。


3. 正则表达式使用示例:


  在 grep 命令中grep '^start.*end$' file.txt 会在 file.txt 中查找以 start 开头,以 end 结尾的行。
  在 sed 命令中:sed's/[0-9]\+/X/g' file.txt 会将 file.txt 中所有的数字序列替换为 X
  在 awk 命令中:awk '/^[a-zA-Z]+$/{print $0}' file.txt 会打印出 file.txt 中只包含字母的行。


4.转义字符:


在正则表达式和通配符中,某些字符具有特殊含义,要匹配它们的字面意义,需要使用转义字符 \

    例如,要匹配 * 本身,在正则表达式中使用 \*,在通配符中可能需要 \* 或单引号将其括起来。


正则表达式练习

#练习文本
John Doe, 25 years old, lives at 123 Main St, Apt 4B, New York, NY 10001.
Jane Smith, 30 years old, lives at 456 Elm St, Apt 7C, Los Angeles, CA 90001.
Mike Johnson, 42 years old, lives at 789 Oak St, Apt 10D, Chicago, IL 60601.
Alice Williams, 28 years old, lives at 321 Pine St, Apt 5E, Houston, TX 77001.
Bob Brown, 35 years old, lives at 654 Maple St, Apt 8F, Philadelphia, PA 19101.
Contact phone numbers:
John: (123) 456-7890
Jane: (234) 567-8901
Mike: (345) 678-9012
Alice: (456) 789-0123
Bob: (567) 890-1234
Emails:
john.doe@example.com
jane.smith@example.org
mike.johnson@example.net
alice.williams@example.co.uk
bob.brown@example.io
Product IDs:
P001, P002, P003, P004, P005
Order numbers:
O12345, O23456, O34567, O45678, O56789
Dates:
2023-01-15, 2022-12-25, 2021-05-10, 2020-08-30, 2019-11-20



编写一个正则表达式,匹配所有的姓名(只匹配名字和姓氏)。

[ldx@VM-20-5-opencloudos ~]$ cat test 
John Doe, 25 years old, lives at 123 Main St, Apt 4B, New York, NY 10001.
Jane Smith, 30 years old, lives at 456 Elm St, Apt 7C, Los Angeles, CA 90001.
Mike Johnson, 42 years old, lives at 789 Oak St, Apt 10D, Chicago, IL 60601.
Alice Williams, 28 years old, lives at 321 Pine St, Apt 5E, Houston, TX 77001.
Bob Brown, 35 years old, lives at 654 Maple St, Apt 8F, Philadelphia, PA 19101.
Contact phone numbers:
John: (123) 456-7890
Jane: (234) 567-8901
Mike: (345) 678-9012
Alice: (456) 789-0123
Bob: (567) 890-1234
Emails:
john.doe@example.com
jane.smith@example.org
mike.johnson@example.net
alice.williams@example.co.uk
bob.brown@example.io
Product IDs:
P001, P002, P003, P004, P005
Order numbers:
O12345, O23456, O34567, O45678, O56789
Dates:
2023-01-15, 2022-12-25, 2021-05-10, 2020-08-30, 2019-11-20

[ldx@VM-20-5-opencloudos ~]$ grep -E '[A-Z][a-z]+\s[A-Z][a-z]+' test | cut -d ',' -f 1  #cut使用方法
John Doe
Jane Smith
Mike Johnson
Alice Williams
Bob Brown

[ldx@VM-20-5-opencloudos ~]$ grep -E '[A-Z][a-z]+\s[A-Z][a-z]+' test | awk '{print $1}'
John
Jane
Mike
Alice
Bob

[ldx@VM-20-5-opencloudos ~]$ grep -E '[A-Z][a-z]+\s[A-Z][a-z]+' test | awk -F ',' '{print $1}'  #awk使用方法
John Doe
Jane Smith
Mike Johnson
Alice Williams
Bob Brown

答案:

[ldx@VM-20-5-opencloudos ~]$ grep -E '[A-Z][a-z]+\s[A-Z][a-z]+' test | awk -F ',' '{print $1}'  #awk使用方法

解释

[A-Z] 匹配一个大写字母,作为名字或姓氏的首字母。

[a-z]+ 匹配一个或多个小写字母,作为名字或姓氏的其余部分。

\s 匹配一个空格。



编写一个正则表达式,匹配所有的年龄(只匹配数字)。

#自己感受下
[ldx@VM-20-5-opencloudos ~]$ cat test
John Doe, 25 years old, lives at 123 Main St, Apt 4B, New York, NY 10001.
Jane Smith, 30 years old, lives at 456 Elm St, Apt 7C, Los Angeles, CA 90001.
Mike Johnson, 42 years old, lives at 789 Oak St, Apt 10D, Chicago, IL 60601.
Alice Williams, 28 years old, lives at 321 Pine St, Apt 5E, Houston, TX 77001.
Bob Brown, 35 years old, lives at 654 Maple St, Apt 8F, Philadelphia, PA 19101.
Contact phone numbers:
John: (123) 456-7890
Jane: (234) 567-8901
Mike: (345) 678-9012
Alice: (456) 789-0123
Bob: (567) 890-1234
Emails:
john.doe@example.com
jane.smith@example.org
mike.johnson@example.net
alice.williams@example.co.uk
bob.brown@example.io
Product IDs:
P001, P002, P003, P004, P005
Order numbers:
O12345, O23456, O34567, O45678, O56789
Dates:
2023-01-15, 2022-12-25, 2021-05-10, 2020-08-30, 2019-11-20

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -E '[A-Z][a-z]'
John Doe, 25 years old, lives at 123 Main St, Apt 4B, New York, NY 10001.
Jane Smith, 30 years old, lives at 456 Elm St, Apt 7C, Los Angeles, CA 90001.
Mike Johnson, 42 years old, lives at 789 Oak St, Apt 10D, Chicago, IL 60601.
Alice Williams, 28 years old, lives at 321 Pine St, Apt 5E, Houston, TX 77001.
Bob Brown, 35 years old, lives at 654 Maple St, Apt 8F, Philadelphia, PA 19101.
Contact phone numbers:
John: (123) 456-7890
Jane: (234) 567-8901
Mike: (345) 678-9012
Alice: (456) 789-0123
Bob: (567) 890-1234
Emails:
Product IDs:
Order numbers:
Dates:

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -E '[A-Z][a-z]' | grep -v ':' 
John Doe, 25 years old, lives at 123 Main St, Apt 4B, New York, NY 10001.
Jane Smith, 30 years old, lives at 456 Elm St, Apt 7C, Los Angeles, CA 90001.
Mike Johnson, 42 years old, lives at 789 Oak St, Apt 10D, Chicago, IL 60601.
Alice Williams, 28 years old, lives at 321 Pine St, Apt 5E, Houston, TX 77001.
Bob Brown, 35 years old, lives at 654 Maple St, Apt 8F, Philadelphia, PA 19101.

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -E '[A-Z][a-z]' | grep -v ':' | cut -d ',' -f 2 
 25 years old
 30 years old
 42 years old
 28 years old
 35 years old
 
[ldx@VM-20-5-opencloudos ~]$ cat test | grep -E '[A-Z][a-z]' | grep -v ':' | cut -d ',' -f 2 | awk '{print $1}'
25
30
42
28
35


答案

cat test | grep -E '[A-Z][a-z]' | grep -v ':' | cut -d ',' -f 2 | awk '{print $1}'

解释

1. cat test:

功能:将文件 test 的内容输出到标准输出。
2. grep -E '[A-Z][a-z]':

功能:使用扩展正则表达式 -E 查找包含一个大写字母后跟一个小写字母的行。
解释:
[A-Z] 匹配一个大写字母。
[a-z] 匹配一个小写字母。
3. grep -v ':':

功能:使用 -v 选项排除包含冒号 : 的行。
解释:
-v 表示反转匹配,即排除满足条件的行,这里是排除包含冒号的行。
4. cut -d ',' -f 2:

功能:使用逗号 , 作为分隔符,提取第二列。
解释:
-d ',' 指定分隔符为逗号。
-f 2 表示提取第二列。
5. awk '{print $1}':

功能:使用 awk 打印输出结果的第一列。
解释:
{print $1} 是 awk 的操作,打印每行的第一个字段。
可能




编写一个正则表达式,匹配所有的街道地址(包括街道名称和门牌号)。

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -E [A-Z][a-z] | grep -v ':' | cut -d ',' -f 3-6
 lives at 123 Main St, Apt 4B, New York, NY 10001.
 lives at 456 Elm St, Apt 7C, Los Angeles, CA 90001.
 lives at 789 Oak St, Apt 10D, Chicago, IL 60601.
 lives at 321 Pine St, Apt 5E, Houston, TX 77001.
 lives at 654 Maple St, Apt 8F, Philadelphia, PA 19101.
 
[ldx@VM-20-5-opencloudos ~]$ cat test | grep -E [A-Z][a-z] | grep -v ':' | awk -F ',' '{print $3,$4,$5,$6}'
 lives at 123 Main St  Apt 4B  New York  NY 10001.
 lives at 456 Elm St  Apt 7C  Los Angeles  CA 90001.
 lives at 789 Oak St  Apt 10D  Chicago  IL 60601.
 lives at 321 Pine St  Apt 5E  Houston  TX 77001.
 lives at 654 Maple St  Apt 8F  Philadelphia  PA 19101.



编写一个正则表达式,匹配所有的公寓号码(如 4B、7C 等)。

[ldx@VM-20-5-opencloudos ~]$ cat test | grep 'Apt' | cut -d ',' -f 4 | awk '{print $2}'
4B
7C
10D
5E



编写一个正则表达式,匹配所有的美国邮政编码(5 位数字)。

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -P [A-Z][a-z] | grep -P "\d{5}" | cut -d ',' -f 6 | awk '{print $2}' | cut -d '.' -f 1
10001
90001
60601
77001
19101



编写一个正则表达式,匹配所有的电话号码(格式为 (xxx) xxx-xxxx)。

[ldx@VM-20-5-opencloudos ~]$ cat test  | grep -P "\(\d{3}\)\s\d{3}-\d{4}"
John: (123) 456-7890
Jane: (234) 567-8901
Mike: (345) 678-9012
Alice: (456) 789-0123
Bob: (567) 890-1234

答案\(\d{3}\)\s\d{3}-\d{4}

解释

    \(\d{3}\) 匹配括号内的三个数字。

    \s 匹配一个空格。

    \d{3}-\d{4} 匹配三个数字、一个短横线和四个数字。




编写一个正则表达式,匹配所有的电子邮件地址。

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -P '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
john.doe@example.com
jane.smith@example.org
mike.johnson@example.net
alice.williams@example.co.uk
bob.brown@example.io

答案[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

解释

    [a-zA-Z0-9._%+-]+ 匹配电子邮件的用户名部分,允许字母、数字和一些特殊字符。

    @ 匹配 @ 符号。

    [a-zA-Z0-9.-]+ 匹配域名的第一部分,允许字母、数字和一些特殊字符。

    \.[a-zA-Z]{2,} 匹配域名的后缀,至少包含两个字母。




编写一个正则表达式,匹配所有的产品 ID(格式为 PXXX,其中 X 是数字)。

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -P "P\d{3}"
P001, P002, P003, P004, P005

答案P\d{3}

解释

    P 匹配 P 字符。

    \d{3} 匹配三个数字。




编写一个正则表达式,匹配所有的订单号(格式为 OXXXXX,其中 X 是数字)。

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -P 'O\d{5}'
O12345, O23456, O34567, O45678, O56789

答案O\d{5}

解释

    O 匹配 O 字符。

    \d{5} 匹配五个数字。




编写一个正则表达式,匹配所有的日期(格式为 YYYY-MM-DD)。

[ldx@VM-20-5-opencloudos ~]$ cat test | grep -P '\d{4}+-\d{2}+-\d{2}'
2023-01-15, 2022-12-25, 2021-05-10, 2020-08-30, 2019-11-20

答案\d{4}-\d{2}-\d{2}

解释

    \d{4} 匹配四个数字,代表年份。

    -\d{2} 匹配一个短横线和两个数字,代表月份。

    -\d{2} 匹配一个短横线和两个数字,代表日期。


正则式拓展

首先正则表达式分为三类(man grep可以看到,分别是basic RegExs,extended RegExs,perl RegExs)

正则表达式:在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。在很多文本编辑器或其他工具里,正则表达式通常被用来检索或替换那些符合某个模式的文本内容。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。

一、正则表达式分类:

    1、基本的正则表达式(Basic Regular Expression 又叫 Basic RegEx  简称 BREs)

    2、扩展的正则表达式(Extended Regular Expression 又叫 Extended RegEx 简称 EREs)

    3、Perl 的正则表达式(Perl Regular Expression 又叫 Perl RegEx 简称 PREs)

    说明:只有掌握了正则表达式,才能全面地掌握 Linux 下的常用文本工具(例如:grep、egrep、GUN sed、 awk 等) 的用法

二、Linux 中常用文本工具与正则表达式的关系 

    常握 Linux 下几种常用文本工具的特点,对于我们更好的使用正则表达式是很有帮助的

grep , egrep 正则表达式特点:

1)grep 支持:BREs、EREs、PREs 正则表达式

grep 指令后不跟任何参数,则表示要使用 ”BREs“ 

grep 指令后跟 ”-E" 参数,则表示要使用 “EREs“

grep 指令后跟 “-P" 参数,则表示要使用 “PREs"

 

2)egrep 支持:EREs、PREs 正则表达式

egrep 指令后不跟任何参数,则表示要使用 “EREs”

egrep 指令后跟 “-P" 参数,则表示要使用 “PREs"

 

3)grep 与 egrep 正则匹配文件,处理文件方法

a. grep 与 egrep 的处理对象:文本文件

b. grep 与 egrep 的处理过程:查找文本文件中是否含要查找的 “关键字”(关键字可以是正则表达式) ,如果含有要查找的 ”关健字“,那么默认返回该文本文件中包含该”关健字“的该行的内容,并在标准输出中显示出来,除非使用了“>" 重定向符号,

c. grep 与 egrep 在处理文本文件时,是按行处理的

 


 正则表达式特点

1)sed 文本工具支持:BREs、EREs

sed 指令默认是使用"BREs"

sed 命令参数 “-r ” ,则表示要使用“EREs"

2)sed 功能与作用

a. sed 处理的对象:文本文件

b. sed 处理操作:对文本文件的内容进行 --- 查找、替换、删除、增加等操作

c. sed 在处理文本文件的时候,也是按行处理的


Awk(gawk)正则表达式特点

1)Awk 文本工具支持:EREs

awk 指令默认是使用 “EREs"

2)Awk 文本工具处理文本的特点

a. awk 处理的对象:文本文件

b. awk 处理操作:主要是对列进行操作

 

三、常见3中类型正则表达式比较

image.png

image.png

image.png

image.png

image.png

image.png

文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码