正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串。vim、grep、awk 、sed 都支持正则表达式,也正是因为由于它们支持正则,才显得它们强大;在以前上班的公司里,由于公司是基于web的服务型网站(nginx),对正则的需求比 较大,所以也花了点时间研究正则,特与大家分享下:

1基础正则表达式
grep 工具,以前介绍过。
grep -[acinv]
'搜索内容串' filename
-a 以文本文件方式搜索
-c 计算找到的符合行的次数
-i 忽略大小写
-n 顺便输出行号

-v 反向选择,即找 没有搜索字符串的行
其中搜索串可以是正则表达式!

1
搜索有the的行,并输出行号

$grep -n 'the' regular_express.txt
搜 索没有the的行,并输出行号
$grep -nv 'the'
regular_express.txt

2 利 用[]搜索集合字符
[] 表示其中的某一个字符 ,例如[ade] 表示a或d或e

woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt
8:I can't
finish the test.
9:Oh! the soup taste good!

可以用^符号做[]内的前缀,表示除[]内的字符之外的字 符。
比如搜索oo前没有g的字符串所在的行. 使用 '[^g]oo'
作搜索字符串
woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt
2:apple
is my favorite food.
3:Football game is not use feet only.
18:google is
the best tools for search keyword.
19:goooooogle yes!

[]
内可以用范围表示,比如[a-z] 表示小写字母,[0-9] 表示0~9的数字, [A-Z] 则是大写字母们。[a-zA-Z0-9]表示所有数字与英文字符。
当然也可以配合^来排除字符。
搜索包含数字的行
woody@xiaoc:~/tmp$ grep -n '[0-9]'
regular_express.txt
5:However ,this dress is about $ 3183 dollars.

15:You are the best is menu you are the no.1.

行首与行尾字符 ^ $. ^
表示行的开头,$表示行的结尾( 不是字符,是位置)那么‘^$' 就表示空行,因为只有
行首和行尾。

这里^与[]里面使用的^意义不同。它表示^后面的串是在行的开头。
比如搜索the在开头的行
woody@xiaoc:~/tmp$
grep -n '^the' regular_express.txt
12:the symbol '*' is represented as star.

搜索以小写字母开头的行
woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt

2:apple is my favorite food.
4:this dress doesn't fit me.

10:motorcycle is cheap than car.
12:the symbol '*' is represented as
star.
18:google is the best tools for search keyword.
19:goooooogle yes!

20:go! go! Let's go.
woody@xiaoc:~/tmp$

搜索开头不是英文字母的行

woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open
Source" is a good mechanism to develop programs.
21:#I am VBird

woody@xiaoc:~/tmp$

$表示它前面的串是在行的结尾,比如 '\.' 表示 . 在一行的结尾
搜索末尾是.的行

woody@xiaoc:~/tmp$ grep -n '\.$' regular_express.txt //. 是正则表达式的特殊符号,所以要用\转义

1:"Open Source" is a good mechanism to develop programs.
2:apple is my
favorite food.
3:Football game is not use feet only.
4:this dress
doesn't fit me.
5:However ,this dress is about $ 3183 dollars.
6:GNU is
free air not free beer.
.....

注意在MS的系统下生成的文本文件,换行会加上一个 ^M
字符。所以最后的字符会是隐藏的^M ,在处理Windows
下面的文本时要特别注意!
可以用cat dos_file | tr -d '\r'
> unix_file 来删除^M符号。 ^M==\r

那么'^$' 就表示只有行首行尾的空行拉!
搜索空行

woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt
22:
23:

woody@xiaoc:~/tmp$

搜索非空行
woody@xiaoc:~/tmp$ grep -vn '^$'
regular_express.txt
1:"Open Source" is a good mechanism to develop programs.

2:apple is my favorite food.
3:Football game is not use feet only.

4:this dress doesn't fit me.
..........

任意一个字符. 与重复字符 *

在bash中*代表通配符,用来代表任意个 字符,但是在正则表达式中,他含义不同,*表示有0个或多个 某个字符。
例如 oo*,
表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.

点. 代表一个任意字符,必须存在。 g??d 可以用
'g..d' 表示。 good ,gxxd ,gabd .....都符合。

woody@xiaoc:~/tmp$ grep -n 'g..d'
regular_express.txt
1:"Open Source" is a good mechanism to develop programs.

9:Oh! the soup taste good!
16:The world is the same with 'glad'.

woody@xiaoc:~/tmp$

搜索两个o以上的字符串
woody@xiaoc:~/tmp$ grep -n 'ooo*'
regular_express.txt //前两个o一定存在,第三个o可没有,也可有多个。
1:"Open Source" is a good
mechanism to develop programs.
2:apple is my favorite food.
3:Football
game is not use feet only.
9:Oh! the soup taste good!
18:google is the
best tools for search keyword.
19:goooooogle yes!

搜索g开头和结尾,中间是至少一个o的字符串,即gog, goog....gooog...等
woody@xiaoc:~/tmp$
grep -n 'goo*g' regular_express.txt
18:google is the best tools for search
keyword.
19:goooooogle yes!

搜索g开头和结尾的字符串在的行
woody@xiaoc:~/tmp$
grep -n 'g.*g' regular_express.txt // .*表示 0个或多个任意字符
1:"Open Source" is a
good mechanism to develop programs.
14:The gd software is a library for
drafting programs.
18:google is the best tools for search keyword.

19:goooooogle yes!
20:go! go! Let's go.

限定连续重复字符的范围 { }

. * 只能限制0个或多个, 如果要确切的限制字符重复数量,就用{范围} 。范围是数字用,隔开 2,5 表示2~5个,
2表示2个,2,
表示2到更多个
注意,由于{ }在SHELL中有特殊意义,因此作为正则表达式用的时候要用\转义一下。

搜索包含两个o的字符串的行。

woody@xiaoc:~/tmp$ grep -n 'o\{2\}' regular_express.txt
1:"Open Source"
is a good mechanism to develop programs.
2:apple is my favorite food.

3:Football game is not use feet only.
9:Oh! the soup taste good!

18:google is the best tools for search keyword.
19:goooooogle yes!

搜索g后面跟2~5个o,后面再跟一个g的字符串的行。
woody@xiaoc:~/tmp$ grep -n 'go\{2,5\}g'
regular_express.txt
18:google is the best tools for search keyword.

搜索包含g后面跟2个以上o,后面再跟g的行。。
woody@xiaoc:~/tmp$ grep -n 'go\{2,\}g'
regular_express.txt
18:google is the best tools for search keyword.

19:goooooogle yes!

注意,相让[]中的^ - 不表现特殊意义,可以放在[]里面内容的后面。

'[^a-z\.!^ -]' 表示没有小写字母,没有. 没有!, 没有空格,没有- 的 串,注意[]里面有个小空格。

另外shell
里面的反向选择为[!range], 正则里面是 [^range]

2扩展正则表达式

扩展正则表达式是对基础正则表达式添加了几个特殊构成的。
它令某些操作更加方便。
比如我们要去除 空白行和行首为 #的行,
会这样用:
woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#'

"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.
............

然而使用支持扩展正则表达式的 egrep 与扩展特殊符号 | ,会方便许多。

注意grep只支持基础表达式, 而egrep 支持扩展的, 其实 egrep 是 grep -E 的别名而已。因此grep -E 支持扩展正则。

那么:
woody@xiaoc:~/tmp$ egrep -v '^$|^#' 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.

....................
这里| 表示或的关系。 即满足 ^$ 或者 ^# 的字符串。

这里列出几个扩展特殊符号:
+, 于 . * 作用类似,表示 一个或多个重复字符。
?, 于 . *
作用类似,表示0个或一个字符。
|,表示或关系,比如 'gd|good|dog' 表示有gd,good或dog的串

(),将部分内容合成一个单元组。 比如 要搜索 glad 或 good 可以这样 'g(la|oo)d'
()的好处是可以对小组使用 + ? *
等。
比如要搜索A和C开头结尾,中间有至少一个(xyz) 的串,可以这样 : 'A(xyz)+C'

grep用法的更多相关文章

  1. grep用法详解:grep与正则表达式【转】

    转自:http://blog.csdn.net/hellochenlian/article/details/34088179 grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配 ...

  2. linux命令之grep用法介绍

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  3. grep用法【转】

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  4. linux中grep用法(“或”、“与”)

    1.普通用法 grep keyword 若果keyword中有空格 grep "keyword" 例如: ls | grep mp4 2.“与”操作(其实就是多次筛选) grep ...

  5. grep用法小结

    用法 grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] -e PATTERN ... [FILE...] grep [OPTIONS] -f FILE . ...

  6. linux中find与三剑客之grep用法

    find用法 find一般用来用来查找文件名称 根据文件的名称或者属性查找文件. 语法格式: find [查找范围] [参数] 参数: -name : 按照文件的名字查找文件 * :通配符 -inam ...

  7. Linux grep用法整理

    grep -i 忽略大小写 grep --color grep -v 反向查找 grep -o 只显示被模式匹配到的字符串

  8. linux之grep用法

    运用场景:在当前目录下查找,比如代码目录,不需要在.svn目录下,以及ctags生成的tags文件中查找: grep的--exclude-dir=参数就是为了排除某个目录的,即不包含等号后面的目录: ...

  9. linux学习:xargs与grep用法整理

    xargs xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具. xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据. xargs 也可 ...

随机推荐

  1. javascript 之 面向对象【创建对象】

    创建对象 (1) 工厂模式 function createPerson(name, age, job){ var o = new Object(); o.name = name; o.age = ag ...

  2. redis 执行操作时提示(error) NOAUTH Authentication required.

    (error) NOAUTH Authentication required. 出现认证问题,设置了认证密码,输入密码即可 127.0.0.1:6379> auth 123456

  3. Flutter安装之后cmd运行错误解决

    当把Flutter环境变量配置之后,打开cmd输入:flutter 出现如下错误: 'MySQL' is not recognized as an internal or external comma ...

  4. Spring循环依赖问题

    什么是循环依赖? 循环依赖就是循环引用,指两个或多个bean互相持有对方,比如说TestA引用TestB.TestB引用TestA,最终形成一个闭环. 注意:循环依赖不是指循环调用. 循环调用:指方法 ...

  5. oledb

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...

  6. 【NET Core】Nuget包发布流程

    1.新建一个.NET Core类库 2.新增一个方法,并编译项目 3.下载Nuget.exe,与刚才新建的类库放在同一目录下 下载地址:https://www.nuget.org/downloads ...

  7. 【转】RTP学习笔记

    转自:https://www.cnblogs.com/yoyotl/p/5650101.html 一.定义 实时传输协议(Real- time Transport Protocol,RTP)是在Int ...

  8. MemSQL Start[c]UP 2.0 - Round 1E. Three strings

    题意:给3个字符串,问从1到min(l1,l2,l3)的长度的子串,找到从该位置长度为l,三个子串相同的三元组的个数 题解:把3个子串用分隔符串起来.然后分开统计每个节点在三个串中出现次数.最后乘起来 ...

  9. MongoVUE的table view视图不显示列标题

    近来项目用到mongodb,遂装了个MongoVUE,当然是破解版的. 但是发现个小问题,就是table view视图下列标题文字标签不见了,Find的执行按钮也是空白一片: 开始以为破解的不彻底,重 ...

  10. 基于gensim的LDA主题模型实现 一键式函数打包

    def genlda(textlist,n): ticks = str(time.time()).replace('.','')[-6:-1] nn=str(n) dictionary = corpo ...