linux 正则表达式使用
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 正则表达式使用的更多相关文章
- linux正则表达式之-基础正则表达式(基于grep)
linux正则表达式: 简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法,如:假设@代表123456,!代表abcde.通过定义的这些特殊符号的铺助,系统管理员就可以快速的过滤,替换或 ...
- 关于清晰讲解linux正则表达式的博文分享
http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html linux shell 正则表达式(BREs,EREs,PREs)差异比 ...
- Linux正则表达式grep与egrep
grep -io "http:/=[A-Z0-9]\{16\}" ./wsxf.txt >wsxf_urls.txt Linux正则表达式grep与egrep 正则表达式:它 ...
- 【Linux】linux正则表达式及通配符
正则表达式就是用于匹配每行输入的一种模式,模式是指一串字符序列.拥有强大的字符搜索功能.也非常方便的搜索过滤出我们想要的内容. linux正则表达式分为基本正则表达式(Basic Regexp)和扩展 ...
- Linux 正则表达式_010
Linux 正则表达式 标注:本教程只针对linux运维的三剑客命令awk,sed,grep正则表达式 什么是正则表达式? 简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法通过定义的这 ...
- linux正则表达式(基础正则表达式+扩展正则表达式)
正则表达式应用非常广泛,例如:php,Python,java等,但在linux中最常用的正则表达式的命令就是grep(egrep),sed,awk等,换句话 说linux三剑客要想能工作的更高效,就一 ...
- linux 正则表达式和通配符
linux 正则表达式和通配符 通配符用于查找文件 包含三种: * ? [] * 代表任意个任意字符 ? 代表任意一个字符 [] 代表中括号中的一个字符 正则表达式(正则是包含匹配,只要包含就可以匹 ...
- 七 、linux正则表达式
为处理大量的字符串而定义的一套规则和方法 1)linux正则表达式以行为单位处理 2)alians grep = “grep –color=auto”,让匹配的内容显示颜色 3)注意字符集,expor ...
- (转)linux正则表达式详解
linux正则表达式详解 http://blog.csdn.net/wuliowen/article/details/64131815 1:什么是正则表达式: 简单的说,正则表达式就是处理字符串的方法 ...
- linux正则表达式企业级深度实践案例1
linux正则表达式结合三剑客企业级实践: 1.取系统ip [root@redhat~]# ifconfig eth0 解答: 替换命令: sed 's#支持正则位置##g' file 先取第 ...
随机推荐
- Linux 关于Transparent Hugepages的介绍
透明大页介绍 Transparent Huge Pages的一些官方介绍资料: Transparent Huge Pages (THP) are enabled by default in RHEL ...
- Selenium-java-web常用操作---2
都是些的方法,一起交流交流 上传文件 private static void action2() { // TODO Auto-generated method stub WebElement ele ...
- BZOJ1878[SDOI2009]HH的项链
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...
- C#简单的对象交互
在对象的世界里,一切皆为对象;对象与对象相互独立,互不干涉,但在一定外力的作用下对象开始共同努力 对象交互的实例 电视机大家都有吧,依照万物皆对象的思维模式来看,电视机可以是一个类,然后电视机有一些基 ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- underscore 源码解读之 bind 方法的实现
自从进入七月以来,我的 underscore 源码解读系列 更新缓慢,再这样下去,今年更完的目标似乎要落空,赶紧写一篇压压惊. 前文 跟大家简单介绍了下 ES5 中的 bind 方法以及使用场景(没读 ...
- json的场景应用与实战
首先 要感谢慕课网的老师 地址:http://www.imooc.com/learn/68 下面我来开始总结: 什么是json的这些我就不多说了 不懂百度 <?php function cre ...
- 基于MemoryCache的缓存辅助类
背景: 1. 什么是MemoryCache? memoryCache就是用电脑内存做缓存处理 2.使用范围? 可用于不常变的数据,进行保存在内存中,提高处理效率 代码: /// <summary ...
- js小练习去掉指定的字符组成一句话输出
今天在codewar做练习题时,要求写一个函数把一个字符串去掉WUB这些多余的字符然后把剩下的组成一句话输出,如传入"WUBAWUBBWUBCWUB"后返回"A B C& ...
- Django补遗(一)
链接MYSQL数据库 修改项目中的配置文件: DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME' ...