1.正则表达式概念

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

正则表达式的基本元素包括普通字符和元字符,例如,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!

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

linux 正则表达式使用的更多相关文章

  1. linux正则表达式之-基础正则表达式(基于grep)

    linux正则表达式: 简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法,如:假设@代表123456,!代表abcde.通过定义的这些特殊符号的铺助,系统管理员就可以快速的过滤,替换或 ...

  2. 关于清晰讲解linux正则表达式的博文分享

    http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html  linux shell 正则表达式(BREs,EREs,PREs)差异比 ...

  3. Linux正则表达式grep与egrep

    grep -io "http:/=[A-Z0-9]\{16\}" ./wsxf.txt >wsxf_urls.txt Linux正则表达式grep与egrep 正则表达式:它 ...

  4. 【Linux】linux正则表达式及通配符

    正则表达式就是用于匹配每行输入的一种模式,模式是指一串字符序列.拥有强大的字符搜索功能.也非常方便的搜索过滤出我们想要的内容. linux正则表达式分为基本正则表达式(Basic Regexp)和扩展 ...

  5. Linux 正则表达式_010

    Linux 正则表达式 标注:本教程只针对linux运维的三剑客命令awk,sed,grep正则表达式 什么是正则表达式? 简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法通过定义的这 ...

  6. linux正则表达式(基础正则表达式+扩展正则表达式)

    正则表达式应用非常广泛,例如:php,Python,java等,但在linux中最常用的正则表达式的命令就是grep(egrep),sed,awk等,换句话 说linux三剑客要想能工作的更高效,就一 ...

  7. linux 正则表达式和通配符

    linux 正则表达式和通配符 通配符用于查找文件 包含三种:  * ? [] * 代表任意个任意字符 ? 代表任意一个字符 [] 代表中括号中的一个字符 正则表达式(正则是包含匹配,只要包含就可以匹 ...

  8. 七 、linux正则表达式

    为处理大量的字符串而定义的一套规则和方法 1)linux正则表达式以行为单位处理 2)alians grep = “grep –color=auto”,让匹配的内容显示颜色 3)注意字符集,expor ...

  9. (转)linux正则表达式详解

    linux正则表达式详解 http://blog.csdn.net/wuliowen/article/details/64131815 1:什么是正则表达式: 简单的说,正则表达式就是处理字符串的方法 ...

  10. linux正则表达式企业级深度实践案例1

    linux正则表达式结合三剑客企业级实践: 1.取系统ip [root@redhat~]#  ifconfig  eth0 解答: 替换命令: sed  's#支持正则位置##g'  file 先取第 ...

随机推荐

  1. 初见SpringMVC

    1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...

  2. django之一些feature

    前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. cookie cookie 是一种发送到客 ...

  3. 前端之ajax

    前端之ajax 本节内容 ajax介绍 原生js实现ajax jquery实现ajax json 跨域请求 1. ajax介绍 AJAX(Asynchronous Javascript And XML ...

  4. codevs 1245 最小的N个和

    1245 最小的N个和 http://codevs.cn/problem/1245/ 题目描述 Description 有两个长度为 N 的序列 A 和 B,在 A 和 B 中各任取一个数可以得到 N ...

  5. C#获取CPU占用率、内存占用、磁盘占用、进程信息

    代码: using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading ...

  6. Android工程的编译过程

    现在很多人想对Android工程的编译和打包进行自动化,比如建立每日构建系统.自动生成发布文件等等.这些都需要我们对Android工程的编译和打包有一个深入的理解,至少要知道它的每一步都做了什么,需要 ...

  7. sed awk grep三剑客常用

    sed的常用用法: awk的常用用法: grep的常用用法: 除了列出符合行之外,并且列出后10行. grep -A 10 Exception kzfinance-front.log 除了列出符合行之 ...

  8. shell及脚本2——shell 环境及命令

    一.快捷键.通配符.特殊符号 1. 快捷键 CTRL+C:终止目前的命令 CTRL+D:输入结束,EOF CTRL+M:ENTER CTRL+S:暂停屏幕输出 CTRL+Q:恢复屏幕输出 CTRL+U ...

  9. hadoop配置

    配置参考博客:(目前有问题,百度说官网的是32bit,现在正在尝试64位 http://www.powerxing.com/install-hadoop-cluster/

  10. 使用Xunit进行单元测试

    http://xunit.github.io/docs/getting-started-desktop.html 1. 新建一个类库项目 2. 通过NuGet引入xunit,Shouldly,xuni ...