Linux之正则表达式1
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。适当使用正则表达式可以提高工作效率。正则表达式帮助文档链接: 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的更多相关文章
- linux shell 正则表达式(BREs,EREs,PREs)差异比较
linux shell 正则表达式(BREs,EREs,PREs)差异比较 则表达式:在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符 串的单个字符串.在很多文本编辑器或其他工具里 ...
- 07: linux中正则表达式与grep使用
1.1 linux中正则表达式 1.^linux 以linux开头的行 2.$php 以php结尾的行 3.. 匹配任意单字符 4..+ ...
- linux shell 正则表达式(BREs,EREs,PREs)的比较
原文 : linux shell 正则表达式(BREs,EREs,PREs)差异比较 在使用 linux shell的实用程序,如awk,grep,sed等,正则表达式必不可少,他们的区别是什么 ...
- linux ls正则表达式
ls就是默认排序的. 所以: ls只支持通配符,不支持正则,所以单纯用ls是不能实现的. 一些正则过滤操作需要结合支持正则的命令如grep.sed或awk. 例如:ls | grep "[0 ...
- 【转】linux shell 正则表达式(BREs,EREs,PREs)差异比较
我想各位也和我一样,再linux下使用grep,egrep, awk , sed, vi的搜索时,会经常搞不太清楚,哪此特殊字符得使用转义字符'\' .. 哪些不需要, grep与egrep的差异 ...
- 3分钟搞定Linux系统正则表达式
正则表达式是一种字符模式,用于在查找过程中匹配制定的字符. 元字符通常在Linux中分为两类:Shell元字符,由Linux Shell进行解析:正则表达式元字符,由vi/grep/sed/awk等文 ...
- linux shell 正则表达式(BREs,EREs,PREs)差异比较(转)
add by zhj: Python的正则表达式跟Perl很像,Python的re模块文档中也说"This module provides regular expression matchi ...
- Linux基础-正则表达式整理---------------grep、sed、awk
目录: Ⅰ:正则表达式 Ⅱ:作业 Ⅰ:正则表达式 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在lin ...
- linux shell 正则表达式(BREs,EREs,PREs)差异比较(转,当作资料查)
转载: 在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或 替换那些符合某个模式的文本内容.许多程序设计语 ...
随机推荐
- 依赖注入demo
让我们看一个例子: class UserProvider{ protected $connection; public function __construct(){ $this->connec ...
- 『Python CoolBook』C扩展库_其六_从C语言中调用Python代码
点击进入项目 一.C语言运行pyfun的PyObject对象 思路是在C语言中提供实参,传给python函数: 获取py函数对象(PyObject),函数参数(C类型) 获取GIL(PyGILStat ...
- JavaScript基础二
1.7 常用内置对象 所谓内置对象就是ECMAScript提供出来的一些对象,我们知道对象都是有相应的属性和方法 1.7.1 数组Array 1.数组的创建方式 字面量方式创建(推荐大家使用这种方式, ...
- Socket中SO_REUSEADDR详解
1.一般来说,一个端口释放后会等待两分钟之后才能再被使用,SO_REUSEADDR是让端口释放后立即就可以被再次使用. SO_REUSEADDR用于对TCP套接字处于TIME_WAIT状态下的sock ...
- springmvc文件上传示例
首先要导包,用的的包是: commons-fileupload-*.*.*.jar commons-io-*.*.jar *号代表版本号 这里给大家分享一下下载链接:https://files.cnb ...
- Ubuntu系统下安装免驱动摄像头
最近想玩一下视频系列的深度学习模型,便网上淘了一个linux下免驱动的摄像头,直接插上usb接口就行,但是一般还不能直接使用,下面将简单说一下如何使用摄像头: 在你的ternimal下输入以下命令: ...
- 高级数据类型---元祖[tuple]
一.Tuple(元组)与列表类似,不同之处在于元组的 元素不能修改,让列表不可以被修改,以保护数据安全 元组 表示多个元素组成的序列 元组 在 Python 开发中,有特定的应用场景 用于存储 一串 ...
- 关于Error during managed flush [Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1]错误
控制台报错: 08:07:09.293 [http-bio-8080-exec-2] ERROR org.hibernate.internal.SessionImpl - HHH000346: Err ...
- mybatis xml配置文件模版
mybatis xml配置文件模版 1.mybatis核心配置文件书写(SqlMapConfig.xml) <?xml version="1.0" encoding=&quo ...
- echarts双y轴折线图柱状图混合实时更新图
先看下效果,自己用ps做了张gif图,发现很好玩啊..不喜勿喷 自己下载个echarts.min.js 直接上代码: <!DOCTYPE html><html><head ...