【Linux】正则表达式

张开发
2026/4/14 23:59:16 15 分钟阅读

分享文章

【Linux】正则表达式
正则表达式正则表达式概述正则表达式作为一个pattern将pattern与要搜索的字符串进行匹配以便查找一个或多个字符串。正则表达式自成体系由普通字符例如字符 a 到 z和元字符组成的文字模式。普通字符没有显式指定为元字符的所有可打印和不可打印字符字符包括所有大写和小写字母、所有数字、所有标点符号和其他一些符号。元字符除了普通字符之外的字符。正则表达式工具vim、grep、less等和程序语言Perl、Python、C等都使用正则表达式。正则表达式分类普通正则表达式扩展正则表示支持更多的元字符。环境准备[harvyCentOS7 ~17:22:06]$catwordsEOFcatcategoryacatconcatenatecbtc1tcCtc-tc.tdogEOF普通字符[harvyCentOS7 ~17:22:19]$catwords|grepcatcatcategory acat concatenate字符集.匹配除换行符\n、\r之外的任何单个字符相等于[^\n\r]。[harvyCentOS7 ~17:22:42]$catwords|grepc.t[…]匹配[...]中的任意一个字符。[harvyCentOS7 ~17:24:11]$catwords|grepc[ab]tcatcategory acat concatenate cbt[a-z] [A-Z] [0-9][a-z]匹配所有小写字母。[A-Z]匹配所有大写字母。[0-9]匹配所有数字。[harvyCentOS7 ~17:25:40]$catwords|grepc[a-z]t[harvyCentOS7 ~17:27:27]$catwords|grepc[A-Z]tcCt[lao[harvyCentOS7 ~17:28:21]$catwords|grepc[0-9]tc1t[harvyCentOS7 ~17:29:12]$catwords|grepc[a-z0-9]tcatcategory acat concatenate cbt c1t[harvyCentOS7 ~17:30:10]$catwords|grepc[a-zA-Z0-9]tcatcategory acat concatenate cbt c1t cCt# 要想匹配-符号将改符号写在第一个位置[harvyCentOS7 ~18:23:18]$catwords|grepc[-a-zA-Z0-9]tcatcategory acat concatenate cbt c1t cCt c-t[^…]匹配除了[...]中字符的所有字符。[harvyCentOS7 ~18:24:41]$catwords|grepc[^ab]tc1t cCt c-t c.t# ^放中间会被当做普通字符[harvyCentOS7 ~18:25:55]$catwords|grepc[a^b]tcatcategory acat concatenate cbt\将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。例如 ‘n’ 匹配字符 ‘n’。\n匹配换行符。序列\\匹配\而\(则匹配(。[harvyCentOS7 ~18:28:16]$catwords|grepc\.t# 匹配普通字符虽然可以匹配但强烈建议不要在前面加\[harvyCentOS7 ~18:28:22]$catwords|grepc\atcatcategory acat concatenate||符号是扩展表达式中元字符指明两项之间的一个选择。要匹配|请使用\|。# 使用egrep或者grep -E 匹配[harvyCentOS7 ~18:29:30]$catwords|egrepcat|dogcatcategory acat concatenate dog# 或者[harvyCentOS7 ~18:31:17]$catwords|grep-Ecat|dogcatcategory acat concatenate dog字符集总结选项描述[[:digit:]]数字 0 1 2 3 4 5 6 7 8 9等同于[0-9][[:xdigit:]]十六进制数字 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f等同于[0-9a-fA-F][[:lower:]]小写字母在 C 语言环境和ASCII字符编码中对应于[a-z][[:upper:]]大写字母在 C 语言环境和ASCII字符编码中对应于[A-Z][[:alpha:]]字母字符[[:lower:]和[[:upper:]]在C语言环境和ASCII字符编码中等同于**[A-Za-z]**[[:alnum:]]字母数字字符[:alpha:]和[:digit:]在C语言环境和ASCII字符编码中等同于**[0-9A-Za-z]**[[:blank:]]或者[[:space:]]空白字符在 C 语言环境中它对应于制表符、换行符、垂直制表符、换页符、回车符和空格。[[:punct:]]标点符号在C语言环境和ASCII字符编码中它对应于! # $ % ()*,-./:;?[]^_{|}~[[:print:]]或者 [[:graph:]]可打印字符 [[:alnum:]]、[[:punct:]]。[[:cntrl:]]控制字符。在 ASCII中 这些字符对应八进制代码000到037和 177 (DEL)。非打印字符终端中不显示的字符例如换行符。字符描述\cx匹配由x指明的控制字符。例如 \cM 匹配一个 Control-M 或回车符。x 的值必须为 A-Z 或 a-z 之一。否则将 c 视为一个原义的 ‘c’ 字符。\f匹配一个换页符。等价于\x0c和\cL。\n匹配一个换行符。等价于\x0a和\cJ。\r匹配一个回车符。等价于\x0d和\cM。\s匹配任何空白字符包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。注意 Unicode 正则表达式会匹配全角空格符。\S匹配任何**非空白字符**。等价于[^ \f\n\r\t\v]。\w匹配字母、数字、下划线。等价于 [A-Za-z0-9_]\W匹配任何非单词字符。等价于[^A-Za-z0-9_]\t匹配一个制表符。等价于\x09和\cI。\v匹配一个垂直制表符。等价于\x0b和\cK。grep 命令支持\w、\W、\s、\S。定位符^匹配行首位置。[harvyCentOS7 ~18:31:39]$catwords|grep^cat$匹配行末位置。[harvyCentOS7 ~18:32:45]$catwords|grepcat$catacat# 查看 /var/log/message Aug 19 14:01 到 Aug 19 14:06 时间段发生的事件[harvyCentOS7 ~18:33:31]$sudocat/var/log/messages|egrep^Apr 14 12:[1-6]# 只包含cat的行[harvyCentOS7 ~18:35:21]$catwords|grep^cat$cat# 排除/etc/profile文件中以#开头的行[harvyCentOS7 ~18:35:56]$cat/etc/profile|grep^[^#]# 查询/etc/profile文件中有效行[harvyCentOS7 ~18:40:15]$cat/etc/profile|egrep-v^#|^$# -v 取反不显示匹配的内容# 查看系统中有哪些仓库[harvyCentOS7 ~18:43:21]$ yum-config-manager|grep^\[|grep-vmain[base][epel][extras][updates]\b匹配一个单词边界。[harvyCentOS7 ~18:44:44]$catwords|grep\bcatcatcategory hellocatkitty[harvyCentOS7 ~18:45:04]$catwords|grepcat\b[harvyCentOS7 ~18:49:59]$catwords|grep\bcat\bcathellocatkitty\B基本不用非单词边界匹配。[harvyCentOS7 ~18:50:51]$catwords|grep\Bcat\B 和 基本不用\匹配一个单词左边界。\匹配一个单词右边界。[harvyCentOS7 ~18:53:33]$catwords|grep\catcatcategory hellocat[harvyCentOS7 ~18:54:49]$catwords|grepcat\catacat hellocat限定次数**匹配前面的子表达式任意次数。[harvyCentOS7 ~18:55:11]$echodgwords[harvyCentOS7 ~18:55:37]$echodoogwords[harvyCentOS7 ~18:55:44]$catwords|grepdo*gdog dg doog是扩展表达式元字符匹配前面的子表达式一次以上次数。[harvyCentOS7 ~18:55:58]$catwords|egrepdogdog doog??是扩展表达式元字符匹配前面的子表达式一次以下次数。[harvyCentOS7 ~18:56:28]$catwords|egrepdo?gdog dg{n}{}是扩展表达式元字符用于匹配特定次数。例如{n}配置n次。[harvyCentOS7 ~18:57:55]$catwords|egrepdo{2}gdoog{m,n}{m,n}是扩展表达式元字符用于匹配次数介于m-n之间。[harvyCentOS7 ~19:00:46]$echodooogwords[harvyCentOS7 ~19:01:03]$echodoooogwords[harvyCentOS7 ~19:01:13]$catwords|egrepdo{2,3}gdoog dooog{m,}{m,}是扩展表达式元字符匹配前面的子表达式m次以上次数**。[harvyCentOS7 ~19:01:35]$catwords|egrepdo{2,}gdoog dooog doooog{,n}{,n}是扩展表达式元字符匹配前面的子表达式n次以下次数。[harvyCentOS7 ~19:04:00]$catwords|egrepdo{,3}gdog dg doog dooog()( )标记一个子表达式。[harvyCentOS7 ~19:04:40]$echodogdogwords[harvyCentOS7 ~19:05:35]$echodogdogdogwords[harvyCentOS7 ~19:05:44]$echodogdogdogdogwords[harvyCentOS7 ~19:05:56]$catwords|egrep(dog){2,3}dogdog dogdogdog dogdogdogdog[harvyCentOS7 ~19:07:08]$catwords|egrep(dog){2,}dogdog dogdogdog dogdogdogdog综合案例如何过滤出以下内容中所有有效IPv4地址0.0.0.01.1.1.111.11.11.111111.111.111.111999.9.9.9 01.1.1.110.0.0.00.1.1.1266.1.1.1248.1.1.1256.1.1.1参考答案[harvyCentOS7 ~19:10:17]$catiptest.txt|egrep\b(([1-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))(\.(([1-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))){3}\b0.0.0.01.1.1.111.11.11.111111.111.111.11110.0.0.00.1.1.1248.1.1.1# 第一个位 [1-9][0-9]? # 1-99 1[0-9]{2} # 100-199 2[0-4][0-9] # 200-249 25[0-5] # 250-255 # 第二位 [1-9]?[0-9] # 0-99 1[0-9]{2} # 100-199 2[0-4][0-9] # 200-249 25[0-5] # 250-255 # 最终代码 \b(([1-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))(\.(([1-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))){3}\b反向引用对一个正则表达式模式或部分模式两边添加圆括号将导致相关匹配存储到一个临时缓冲区中所捕获的每个子匹配都按照在正则表达式模式中从左到右出现的顺序存储。缓冲区编号从 1 开始最多可存储 99 个捕获的子表达式。每个缓冲区都可以使用\N访问其中N为一个标识特定缓冲区的一位或两位十进制数。\N这用引用方式称之为反向引用。其他工具使用案例vim搜索和替换。less搜索。

更多文章