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 先取第 ...
随机推荐
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...
- linux查看cpu 命令
总核数 = 物理CPU个数 * 每颗物理CPU的核数 总逻辑CPU数 = 物理CPU个数 * 每颗物理CPU的核数 * 超线程数 查看物理CPU个数 cat /proc/cpuinfo| grep & ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 当AngularJS POST方法碰上PHP
问题描述 怎么POST过去给PHP都收不到资料? $_POST方法取不到正确的传入值! 原理说明 AngularJS这套framework使用的AJAX方法中,资料传递的格式为JSON,送出去的hea ...
- 高品质开源工具Chloe.ORM:支持存储过程与Oracle
扯淡 这是一款高质量的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq.借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询.聚合查询.插入数据.批量删 ...
- Ubuntu 16.04 LAMP server 指南 - 配置 Apache2.4,PHP7,和MariaDB(而不是MySQL)
翻译自:https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ 昨天在虚 ...
- HTML中为何p标签内不可包含div标签?那哪些块元素里面不能放哪些块元素呢?
先看下面的例子你就能明白两者的差别: <p>测试一下块元素与<span>内联元素</span>的差别</p> <p>测试一下<div& ...
- celery 框架
转自:http://www.cnblogs.com/forward-wang/p/5970806.html 生产者消费者模式 在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据 ...