Linux 正则表达式使用简介

校睿宝培训机构ERP,学员课时管理软件
实验预约管理软件,实验室管理软件,大学、高校实验预约平台
网站日志分析软件,网站访问日志在线分析
哲涛服务器监控软件,IT运维管理软件,CPU、内存、磁盘监控软件

正则表达式基本介绍

正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑里,正则表达式通常被用来检索、替换那些符合某个模式的文本。

正则表达式的基本元素包括普通字符和元字符,例如,a、b、1、2 等字符属于普通字符,普通字符可以按照字面意思理解,如:a 只能理解为英文的小写字母a,没有其他隐藏含义。而*、^、[]等元字符,Shell赋予了它们超越字面意思的意义,如:*符号的字面意义只是一个符号,而实际上却表示了重复前面的字符0 次或多次的隐藏含义。因此,掌握正则表达式基本元素主要是对正则表达式中元字符意义的掌握。POSIX 标准将正则表达式分为两类:基本的正则表达式和扩展的正则表达式。

2.grep 概念

grep的全称是globalsearch regular expression(RE)andprint out the line,翻译过来就是全面搜索正则表达式并把行打印出来。其实grep并不是一个单独的程序,而是一个家族。包括grep、egrep和fgrep。egrep和fgrep在使用上与grep并没有显著的不同。egrep是grep的扩展,扩展的正则表达式。

3.grep的使用格式

grep [OPTIONS] PATTERN [FILE...]

常用的[OPTIONS]有以下几个:

-i:匹配时忽略大小写

–color:将匹配的字符串加以颜色显示

-v:显示未被模式匹配到的行

-o:只显示被模式匹配到的字符串

-A#: 表示在匹配的行后将其下面的#行也显示出来

-B#: 表示在匹配的行后将其前面的#行也显示出来

-C#: 表示在匹配的行后将其前后的#行也显示出来

-E:使用扩展正则表达式

基本正则表达式:

以下实例中使用的文本regular_express.txt内容是鸟哥网站上复制过来的。其内容如下。

“Open Source” is a good mechanismto develop programs.

apple is my favorite food.

Football game is not use feet only.

this dress doesn’t fit me.

However, this dress is about $ 3183 dollars.

GNU is free air not free beer.

Her hair is very beauty.

I can’t finish the test.

Oh! The soup taste good.

motorcycle is cheap than car.

This window is clear.

the symbol ‘*’ is represented as start.

Oh! Mygod!

The gd software is a library for draftingprograms.

You are the best is mean you are the no. 1.

The world <Happy> is the same with”glad”.

I like dog.

google is the best tools for search keyword.

goooooogle yes!

go! go! Let’s go.

# I am VBird

使用的grep和egrep命令显示的颜色是在用户的~/.bashrc目录下定义的别名

alias grep=’grep –color’

alias egrep=’egrep –color’

元字符:

.:匹配任意单个字符

例如:

[root@localhostdata]# grep  ‘g..g’ regular_express.txt

google is the best tools for search keyword.

*:表示匹配其前面的字符任意次

例如:

[root@localhostdata]# grep ‘goo*g’ regular_express.txt

google is the best tools for search keyword.

goooooogle yes!

.*:任意长度的任意字符

例如:

[root@localhostdata]# grep ‘goo.*g’ regular_express.txt

“OpenSource” is a good mechanism to develop programs.

google is thebest tools for search keyword.

goooooogle yes!

\?:匹配其前面的字符1次或0次

例如:

[root@localhostdata]# grep ‘gooo\?g’ regular_express.txt

google is thebest tools for search keyword.

\+:其前面的字符至少出现一次

例如:

[root@localhostdata]# grep ‘goo\+’ regular_express.txt

“OpenSource” is a good mechanism to develop programs.

Oh!The soup taste good.

google is thebest tools for search keyword.

goooooogle yes!

\{m\}:精确匹配其前面的字符为m次

例如:

[root@localhostdata]# grep ‘goo\{2\}’ regular_express.txt

goooooogleyes!

\{m,n\}:匹配其前面的字符至少m次,至多n次

例如:

[root@localhostdata]# grep ‘goo\{2,5\}’ regular_express.txt

goooooogle yes!

[]:指定匹配范围内的任意单个字符

例如:

[root@localhostdata]# grep ‘[HEF]‘ regular_express.txt

Footballgame is not use feet only.

However,this dress is about $ 3183 dollars.

Her hair isvery beauty.

Theworld <Happy>is the same with “glad”.

[^]:指定匹配范围外的任意单个字符

例如:

[root@localhostdata]# tail -2 regular_express.txt|grep ‘[^A-Za-z]‘

# I amVBird

以下是一些常用的特殊字符,就不一一举例了

[[:space:]]:其中[:space:]表示空白字符这个范围,再加上一个[]表示匹配空白字符

[[:punct:]]:标点符号

[[:lower:]]:小写字母

[[:upper:]]:大写字母

[[:alpha:]]:大小写字母

[[:digit:]]:数字

[[:alnum:]]:数字和大小写字母

针对位置锚定:

^:锚定行首,表示^后面的任意内容必须出现在行首

例如:

[root@localhostdata]# grep ‘^go’ regular_express.txt

google isthe best tools for search keyword.

goooooogleyes!

go! go!Let’s go.

$:锚定行尾,表示$前面的任意内容必须出现在行尾

^$:表示空白行

例如:

[root@localhostdata]# grep ‘^$’ regular_express.txt |wc -l

\<或\b:锚定词首,其后面的任意字符必须作为单词首部出现

\>或\b:锚定词尾,其前面的任意字符必须作为单词尾部出现

例如:

[root@localhostdata]# grep ‘^\<root\>’ /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@localhostdata]# grep ‘^\broot\b’ /etc/passwd

root:x:0:0:root:/root:/bin/bash

分组:

\(\):例如\(ab\)*表示匹配ab可以出现任意次,代表匹配ab这个组合,主要后向引用)

例如:

[root@localhostdata]# grep “\(ab\)\{2,3\}” ./fith.txt

abababx

ababababxy

xyababaxbababy

后向引用:模式中,如果使用\(\)实现了分组,在某行文本的检查中,如果\(\)的模式匹配到某内容,此内容后面的模式可以被引用

\1:表示第1个小括号中出现的内容

\2:表示第2个小括号中出现的内容

….

例如:

[root@localhostdata]# cat  test

helove his lover

shelike her lover

helike is liker

shelove her liker

helike him

[root@localhostdata]# grep ‘\(l..e\).*\1′ test

helove his lover

helike is liker

此处小括号是(l..e),\1就表示前面小括号出现的是love,\1就代表是love,小括号

中是like,\1就代表是like

 

 

扩展正则表达式:

扩展正则表达式,大部分元字符等与基本的正则表达式都相同,一些特殊的如下:

?:不需要使用\,匹配其前面的字符1次或0次

例如:

[root@localhostdata]# egrep ‘goooo?’ regular_express.txt

goooooogle yes!

+:其前字符至少一次,相当于基本正则表达式的\{1,\}

例如:

[root@localhostdata]# egrep ‘goo+’ regular_express.txt

“OpenSource” is a good mechanism to develop programs.

Oh!The soup taste good.

google is thebest tools for search keyword.

goooooogle yes!

{m,n}:不需要使用\,匹配其前面的字符至少m次,至多n次

例如:

[root@localhostdata]# egrep ‘go{4,6}’ regular_express.txt

goooooogle yes!

():分组

例如:

[root@localhostdata]# egrep ‘(oo){3,5}’ regular_express.txt

goooooogle yes!

|:或者 如a|b,a或者b,是整个左侧或右侧

ab|cd:代表ab或cd

a(b|c)d

 

fgrep:不支持正则表达式:

后面跟的任意模式都帮他当作字符去匹配,其速度较快,如果不需要正则表达式搜索,则可以使用它

例如:

[root@localhostdata]# fgrep –color ‘gooo’ regular_express.txt

goooooogleyes!

 

以上简单的列出了正则表达式的一些使用方法,要熟练掌握,还需要多加练习


出处http://sepizi.blog.51cto.com/9773154/1596631  

SUM服务器监控软件可监控windows,Linux,Unix,aix等系统

校睿宝培训机构ERP,学员课时管理软件
实验预约管理软件,实验室管理软件,大学、高校实验预约平台
网站日志分析软件,网站访问日志在线分析
哲涛服务器监控软件,IT运维管理软件,CPU、内存、磁盘监控软件

上海哲涛网络科技有限公司版权所有 © 2005-2023       沪ICP备06058430号-1

沪公网安备 31011302000898号

点我咨询