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)差异比较(转,当作资料查)
转载: 在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或 替换那些符合某个模式的文本内容.许多程序设计语 ...
随机推荐
- Coding daily
@2017-7月 1可视化控件的awakeFromNib不调用? 因为用代码注册了cell 2scrollView添加子控件布局无效? 最好不要用masonry,直接用frame 还有tableVie ...
- Spring Boot配置加载顺序
如果加载的配置有重复的,它们的加载顺序是这样的,数字越小的优先级越高,即优先级高的覆盖优先级低的配置. Devtools global settings properties on your home ...
- Idea破解注册码
Idea破解注册码 ThisCrackLicenseId-{ "licenseId":"ThisCrackLicenseId", "licenseeN ...
- 出现Unable to locate appropriate constructor on class 错误可能的原因
1)参数构造器的参数类型是否正确2)参数构造器的顺序和hql中的顺序是否一致3)参数构造器的参数个数是否和hql中的个数一致4)参数构造器的参数类型是否TimeStamp
- c# Excel 行高、列宽、合并单元格、单元格边框线、冻结
http://www.2cto.com/kf/201007/52724.html 新增选择整列: Range = ((Range)(WALeTieSheet.Cells[1, PwCStartCol_ ...
- ceph crush的问题
ceph crush的问题看一遍忘一遍,现将<ceph源码分析>一书中相关章节摘抄如下: 4.2.1 层级化的Cluster Map例4-1 Cluster Map定义层级化的Cluste ...
- .net Monitor产生SynchronizationLockException异常的原因
有时在使用Monitor进行并发同步编程时,会产生SynchronizationLockException异常,抛出的异常内容是"Object synchronization method ...
- boot项目swagger接口调试工具默认访问路径
今天第一次接触boot项目,项目里集成了swagger接口调试工具,以前写项目的时候在swagger 文件夹里可以找到访问路径,换成boot项目以后找不到swagger文件夹了,百度了一下,boot项 ...
- Python编程--类的分析
一.类的概念 python是面向对象的编程语言,详细来说,我们把一类相同的事物叫做类,其中用相同的属性(其实就是变量描述),里面封装了相同的方法,比如:汽车是一个类,它包括价格.品牌等属性.那么我们如 ...
- fullpage 中输入框弹起 页面上移问题处理
fullpage页面要是有输入框的话 点击输入框 键盘弹出的时候会把输入框也顶起来 页面就会向上移 但是键盘收回的时候页面还是上移的状态 对于这个问题只在android手机上出现 为了解决这个问题 ...