3分钟搞定Linux系统正则表达式


元字符通常在

Linux

中分为两类:Shell元字符,由Linux Shell进行解析;

正则表达式元字符,由vi/grep/sed/awk等文本处理工具进行解析;

正则表达式一般以文本行进行处理,在进行下面实例之前,先为grep命令设置–color参数:

class="">$aliasgrep='grep--color=auto'

这样每次过滤出来的字符串都会带色彩了。

在开始之前还需要做一件事情,就是创建一个测试用的re-file文件,内容如下:

class="">$catre-fileIhadalovelytimeonourlittlepicnic.
Loverswereallaroundus.Itisspringtime.Oh
love,howmuchIadoreyou.Doyouknow
theextentofmylove?Oh,bytheway,Ithink
Ilostmyglovessomewhereoutinthatfieldof
clover.Didyouseethem?Icanonlyhopelove.
isforever.Iliveforyou.It'shardtogetbackinthe
groove.
class="">$catlinux.txtLinuxisagood
godassdxwbcvnbvbjk
greattttttwexcvxc
operaaaatingdhfghfvx
gooodfsawrerdxxhkl
gdsystemawxxxx
glad
good


正则表达式元字符

这样每次过滤出来的字符串都会带色彩了。

在开始之前还需要做一件事情,就是创建一个测试用的re-file文件,内容如下:


正则表达式元字符


特殊的元字符


扩展的正则表达式


实操



匹配以love开头的所有行

class="">$grep'^love're-filelove,howmuchIadoreyou.Doyouknow



匹配love结尾的所有行

class="">$grep'love$'re-fileclover.Didyouseethem?Icanonlyhopelove.



匹配以l开头,中间包含两个字符,结尾是e的所有行

class="">$grep'l..e're-fileIhadalovelytimeonourlittlepicnic.
love,howmuchIadoreyou.Doyouknow
theextentofmylove?Oh,bytheway,Ithink
Ilostmyglovessomewhereoutinthatfieldof
clover.Didyouseethem?Icanonlyhopelove.
isforever.Iliveforyou.It'shardtogetbackinthe



匹配0个或多个空行,后面是love的字符

class="">$grep'*love're-fileIhadalovelytimeonourlittlepicnic.
love,howmuchIadoreyou.Doyouknow
theextentofmylove?Oh,bytheway,Ithink
Ilostmyglovessomewhereoutinthatfieldof
clover.Didyouseethem?Icanonlyhopelove.



匹配love或Love

class="">$grep'[Ll]ove're-file#对l不区分大小写Ihadalovelytimeonourlittlepicnic.
Loverswereallaroundus.Itisspringtime.Oh
love,howmuchIadoreyou.Doyouknow
theextentofmylove?Oh,bytheway,Ithink
Ilostmyglovessomewhereoutinthatfieldof
clover.Didyouseethem?Icanonlyhopelove.



匹配A-Z的字母,其次是ove

class="">$grep'[A-Z]ove're-fileLoverswereallaroundus.Itisspringtime.Oh



匹配不在A-Z范围内的任何字符行,所有的小写字符

class="">$grep'[^A-Z]'re-fileIhadalovelytimeonourlittlepicnic.
Loverswereallaroundus.Itisspringtime.Oh
love,howmuchIadoreyou.Doyouknow
theextentofmylove?Oh,bytheway,Ithink
Ilostmyglovessomewhereoutinthatfieldof
clover.Didyouseethem?Icanonlyhopelove.
isforever.Iliveforyou.It'shardtogetbackinthe
groove.



匹配love

class="">$grep'love.'re-fileclover.Didyouseethem?Icanonlyhopelove.



匹配空格

class="">$grep'^$'re-file



匹配任意字符

class="">$grep'.*'re-fileIhadalovelytimeonourlittlepicnic.
Loverswereallaroundus.Itisspringtime.Oh
love,howmuchIadoreyou.Doyouknow
theextentofmylove?Oh,bytheway,Ithink
Ilostmyglovessomewhereoutinthatfieldof
clover.Didyouseethem?Icanonlyhopelove.
isforever.Iliveforyou.It'shardtogetbackinthe
groove.



前面o字符重复2到4次

class="">$grep'o{2,4}'re-filegroove.



重复o字符至少2次

class="">$grep'o{2,}'re-filegroove.



重复0字符最多2次

class="">$grep'o{,2}'re-fileIhadalovelytimeonourlittlepicnic.
Loverswereallaroundus.Itisspringtime.Oh
love,howmuchIadoreyou.Doyouknow
theextentofmylove?Oh,bytheway,Ithink
Ilostmyglovessomewhereoutinthatfieldof
clover.Didyouseethem?Icanonlyhopelove.
isforever.Iliveforyou.It'shardtogetbackinthe
groove.

开发云主机域名

重复前一个字符一个或一个以上

class="">$egrep"go+d"linux.txtLinuxisagood
godassdxwbcvnbvbjk
gooodfsawrerdxxhkl
good#####0个或者一个字符ansheng@Ubuntu:/tmp$egrep"go?d"linux.txt
godassdxwbcvnbvbjk
gdsystemawxxxx

或,查找多个字符串

class="">$egrep"gd|good"linux.txtLinuxisagood
gdsystemawxxxx
good



分组过滤匹配

class="">$egrep"g(la|oo)d"linux.txtLinuxisagood
glad
good


实操



匹配以love开头的所有行



匹配love结尾的所有行



匹配以l开头,中间包含两个字符,结尾是e的所有行



匹配0个或多个空行,后面是love的字符



匹配love或Love



匹配A-Z的字母,其次是ove



匹配不在A-Z范围内的任何字符行,所有的小写字符



匹配love



匹配空格



匹配任意字符



前面o字符重复2到4次



重复o字符至少2次



重复0字符最多2次



重复前一个字符一个或一个以上

或,查找多个字符串



分组过滤匹配

相关推荐: 2020年最新Discuz视频教程有哪些推荐

这篇文章将为大家详细讲解有关2020年最新Discuz视频教程有哪些推荐,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Discuz国内领先的开源社区论坛程序,很多知名的互联网公司都是使用的Discuz程序,无论…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 03/31 12:10
下一篇 03/31 12:10