正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。适当使用正则表达式可以提高工作效率。正则表达式帮助文档链接: https://pan.baidu.com/s/1Sws9HBQSR4XSJQZ1dm9G0w 密码: 178u

我们使用的regular_express.txt如下所示

"Open Source" is a good mechanism to 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 $ 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! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. .
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

用cat -An regular_express.txt如下(至于为什么有^M$请转至windows与linux换行规则):

vbird@Ubuntu1604:~$ cat -An regular_express.txt.bak
"Open Source" is a good mechanism to 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 $ dollars.^M$
GNU is free air not free beer.^M$
Her hair is very beauty.^M$
I can't finish the test.^M$
Oh! The soup taste good.^M$
motorcycle is cheap than car.$
This window is clear.$
the symbol '*' is represented as start.$
Oh!^IMy god!$
The gd software is a library for drafting programs.^M$
You are the best is mean you are the no. .$
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$
$

基础正则表示法

普通字符:字母、数字、汉字、下划线、以及后边章节中没有特殊定义的标点符号,都是"普通字符"。表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的一个字符。见例1

简单的转义字符:一些不便书写的字符,采用在前面加 "\" 的方法。还有其他一些在后边章节中有特殊用处的标点符号,在前面加 "\" 后,就代表该符号本身。比如:^, $ 都有特殊意义,如果要想匹配字符串中 "^" 和 "$" 字符,则表达式就需要写成 "\^" 和 "\$"。

表达式 可匹配 表达式 可匹配
\r,\n 回车或换行符 \^ 可匹配^本身
\t 制表符 \$ 可匹配$本身
\\ 代表\本身 \.   可匹配.本身
\d 匹配1个数字字符,等于[0-9] \D 匹配1个非数字字符,等于[^0-9]
\w 匹配包括下划线的任何单词字符。等价于“[A-Za-z0-9_] \W 匹配任何非单词字符和下划线。等价于“[^A-Za-z0-9_]

自定义能够匹配"多种字符"的表达式: 使用方括号 [ ] 包含一系列字符,能够匹配其中任意一个字符。用 [^ ] 包含一系列字符,则能够匹配其中字符之外的任意一个字符。同样的道理,虽然可以匹配其中任意一个,但是只能是一个,不是多个。见例2、例3、例4、例5

表达式 可匹配
[abcd] 可匹配adcd中的任何1个,有且只有1个
[^abc] 可匹配不是abc的任意字符
[f-k] 可匹配"f"~"k"中任意字符
[^A-F0-5] 可匹配"A"~"F"、"0"~"5"之外的任何字符
x|y 匹配x或y。例如,“z|food”能匹配“z”或“food”。“(z|f)ood”则匹配“zood”或“food

代表抽象意义的特殊符号:见例6.

表达式 作用
^ 行首,不匹配任何字符(例6、例7)
$ 行尾,不匹配任何字符(例7)
. 小数点可以匹配除了换行符(\n)以外的任意一个字符,有且只有一个(例8)
* 重复前一个字符 0 到无穷多次(例9)

修饰匹配次数的特殊符号:

表达式 作用
{n} 表达式重复n次,"A\{2\}",相当于"AA"(例10)
{m,n} 表达式重复m~n次,"AB\{2,4\}",相当于"ABB"、"ABBB"、"ABBBB"(例12)
{n,} 表达式至少重复n次,"AB\{2,\}",相当于"ABB"、"ABBB".....(例11)
+ 重复1个或1个以上的前一个RE字符,"go+d",相当于"god"、"good"、"goood"。等于"go{1,}d"
? 重复0个或1个前一个RE字符,"g?d",相当于"gd"、"god"。等于"go{0,1}d"

例1.

vbird@Ubuntu1604:~$ grep -n "the" regular_express.txt
:I can't finish the test.
:the symbol '*' is represented as start.
:You are the best is mean you are the no. .
:The world <Happy> is the same with "glad".
:google is the best tools for search keyword.

例2.

vbird@Ubuntu1604:~$ grep -n "t[ae]st" regular_express.txt
:I can't finish the test.
:Oh! The soup taste good.

例3.

vbird@Ubuntu1604:~$ grep -n "[^g]oo" regular_express.txt
:apple is my favorite food.
:Football game is not use feet only.
:google is the best tools for search keyword. //google不属于匹配到,但是可以匹配到tools,grep是以行为处理单位.
:goooooogle yes! //goooooogle中有多oo,oo前面有o,所以是可以匹配上的

例4.

vbird@Ubuntu1604:~$ grep -n "[^a-z]oo" regular_express.txt  //查找"oo"前不是小写字符的字符串.
:Football game is not use feet only.

例5.

vbird@Ubuntu1604:~$ grep -n "[0-9]" regular_express.txt
:However, this dress is about $ dollars.
:You are the best is mean you are the no. .

例6.

vbird@Ubuntu1604:~$ grep -n "^[^a-zA-Z0-9]" regular_express.txt   //[]外的^代表行首意思,[]内的^代表反向选择,这个表达式意思是查找行首既不是字母也不是数字的字符串
:"Open Source" is a good mechanism to develop programs.
:# I am VBird

例7.

vbird@Ubuntu1604:~$ grep -n "^$" regular_express.txt   //找出空白行
:

例8.

vbird@Ubuntu1604:~$ grep -n "g..d" regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:Oh! The soup taste good.
:The world <Happy> is the same with "glad".

例9.

vbird@Ubuntu1604:~$ grep -n "ooo*" regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:apple is my favorite food.
:Football game is not use feet only.
:Oh! The soup taste good.
:google is the best tools for search keyword.
:goooooogle yes!

例10.

vbird@Ubuntu1604:~$ grep -n "go\{2\}d" regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:Oh! The soup taste good.

例11.

vbird@Ubuntu1604:~$ grep -n "go\{2,\}" regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:Oh! The soup taste good.
:google is the best tools for search keyword.
:goooooogle yes!

例12.

vbird@Ubuntu1604:~$ grep -n "go\{2,3\}g" regular_express.txt
:google is the best tools for search keyword.

综合例子.在文件/etc/manpath.config中,去除空白行和以#开头行,然后查找还有"opt"字符串的数据.

vbird@Ubuntu1604:~$ grep -v "^$" /etc/manpath.config | grep -v "^#" | grep "opt"
MANPATH_MAP /opt/bin /opt/man
MANPATH_MAP /opt/sbin /opt/man
MANDB_MAP /opt/man /var/cache/man/opt

Linux之正则表达式1的更多相关文章

  1. linux shell 正则表达式(BREs,EREs,PREs)差异比较

    linux shell 正则表达式(BREs,EREs,PREs)差异比较 则表达式:在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符 串的单个字符串.在很多文本编辑器或其他工具里 ...

  2. 07: linux中正则表达式与grep使用

    1.1 linux中正则表达式 1.^linux        以linux开头的行 2.$php         以php结尾的行 3..                匹配任意单字符 4..+  ...

  3. linux shell 正则表达式(BREs,EREs,PREs)的比较

    原文 :   linux shell 正则表达式(BREs,EREs,PREs)差异比较 在使用 linux shell的实用程序,如awk,grep,sed等,正则表达式必不可少,他们的区别是什么 ...

  4. linux ls正则表达式

    ls就是默认排序的. 所以: ls只支持通配符,不支持正则,所以单纯用ls是不能实现的. 一些正则过滤操作需要结合支持正则的命令如grep.sed或awk. 例如:ls | grep "[0 ...

  5. 【转】linux shell 正则表达式(BREs,EREs,PREs)差异比较

    我想各位也和我一样,再linux下使用grep,egrep, awk , sed, vi的搜索时,会经常搞不太清楚,哪此特殊字符得使用转义字符'\' ..   哪些不需要, grep与egrep的差异 ...

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

    正则表达式是一种字符模式,用于在查找过程中匹配制定的字符. 元字符通常在Linux中分为两类:Shell元字符,由Linux Shell进行解析:正则表达式元字符,由vi/grep/sed/awk等文 ...

  7. linux shell 正则表达式(BREs,EREs,PREs)差异比较(转)

    add by zhj: Python的正则表达式跟Perl很像,Python的re模块文档中也说"This module provides regular expression matchi ...

  8. Linux基础-正则表达式整理---------------grep、sed、awk

    目录:    Ⅰ:正则表达式    Ⅱ:作业 Ⅰ:正则表达式 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在lin ...

  9. linux shell 正则表达式(BREs,EREs,PREs)差异比较(转,当作资料查)

    转载: 在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或 替换那些符合某个模式的文本内容.许多程序设计语 ...

随机推荐

  1. js没事干应该瞧瞧的一些博客

    https://zhidao.baidu.com/question/1736568808722198187.html http://www.cnblogs.com/webpush/p/4963002. ...

  2. 『OpenCV3』霍夫变换原理及实现

    霍夫变换常用于检测直线特征,经扩展后的霍夫变换也可以检测其他简单的图像结构. 在霍夫变换中我们常用公式 ρ = x*cosθ + y*sinθ 表示直线,其中ρ是圆的半径(也可以理解为原点到直线的距离 ...

  3. [luogu P3369]【模板】普通平衡树(Treap/SBT)

    [luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...

  4. JAVA的原子性和可见性,线程同步的理解

    1.原子性 (1)原子是构成物质的基本单位(当然电子等暂且不论),所以原子的意思代表着——“不可分”: (2)原子性是拒绝多线程操作的,不论是多核还是单核,具有原子性的量,同一时刻只能有一个线程来对它 ...

  5. 各大型网站架构分析收集-原网址http://blog.csdn.net/lovingprince/article/details/3379710

    1. PlentyOfFish 网站架构学习http://www.dbanotes.net/arch/plentyoffish_arch.html 采取 Windows 技术路线的 Web 2.0 站 ...

  6. Linq中datetime的处理以及asp.net下拉列表控件的selectitem,text等的设置显示处理

    dhl:报错:LINQ to Entities 不支持指定的类型成员“Date” Linq如: var v = from l in _dal.Share where l.PingcoId == pin ...

  7. 31 位域、空类的sizeof值

    1 分析下列程序: #include<iostream> using namespace std; struct s { int x: 3; int y: 4; int z: 5; dou ...

  8. 微服务-网关-node.js by 大雄daysn

    目录 序言 一.node.js入门1.1 下载并安装1.2 从helloworld到一个web应用1.3 Express框架二.node.js搭建网关 三.node.js集群搭建   序言 首先一个问 ...

  9. 调用shutdown.sh后出现could not contact localhost8005 tomcat may not be running报错问题

    之前调用tomcat的shutdown.sh无法关闭tomcat,一直报could not contact localhost8005 tomcat may not be running错. 在网上找 ...

  10. html页面调用js文件里的函数报错-->方法名 is not defined处理方法

    前几天写了一个时间函数setInterval,然后出现了这个错误:Uncaught ReferenceError: dosave is not defined(…) 找了半天都没发现错在哪,最后找到解 ...