正则表达式基础->
描述:(grep)
正则表达式是一种字符模式,用于在查找过程中匹配指定的字符。在大多数程序里,正则表达式都被置于两个正斜杠之间,它匹配被查找的行中任何位置出现的相同模式
基础正则表达式
| 正则表达式 | 描述 |
| \ | 转义字符,将特殊字符进行转义,忽略其特殊意义 |
| ^ | 匹配行首,awk中,^是匹配字符串的开始 |
| $ | 匹配行尾,awk中,$是匹配字符串的结尾 |
| ^$ | 表示空行 |
| . | 匹配除换行符\n之外的任意一个字符 |
| [ ] | 匹配包含在[字符]之中的任意单个字符 |
| [^ ] | 匹配[^字符]之外的任意一个字符 |
| [ - ] | 匹配 [ ] 指定范围内的任意一个字符 |
| ? | 匹配之前的项目1次或者0次 |
| + | 匹配之前的项1次或多次 |
| * | 匹配之前的项0次或多次,.* |
| ( ) | 匹配表达式,创建一个用于匹配的子串 |
| { n } | 匹配之前的项至少需要匹配n次 |
| {n,} | 之前的项至少需要匹配n次 |
| {n,m} | 指定之前的项至少匹配n次,最多匹配m次,n<=m |
| | |
交替匹配 | 两边的任意一项ab(c|d)匹配abc 或 abd |
特定字符:
>[[:space:]] 空格
[[:digit:]] [0-9]
[[:lower:]] [a-z]
[[:upper:]] [A-Z]
[[:alpha:]] [a-Z]
正则表达式练习
[root@web02 ]# cat test.txt (练习文本)
I am timfly student!
I learn linux is very happy.
test I like badminton ball ,billiard ball and chinese chess!
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com
my qq num is .
not .
命令:
#过滤率以m开头,以m结尾的行
[root@web02 ]# grep "^m" test.txt
my blog is http://timfly.blog.51cto.com
my qq num is .
[root@web02 ]# grep "m$" test.txt
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com #排除空行,并打印行号
[root@web02 ]# grep -vn "^$" test.txt
:I am timfly student!
:I learn linux is very happy.
:test
:I like badminton ball ,billiard ball and chinese chess!
:my blog is http://timfly.blog.51cto.com
:our site is http://www.timfly123.com
:my qq num is .
:not . #匹配任意一个字符,但不包括空行
[root@web02 ]# grep "." test.txt
I am timfly student!
I learn linux is very happy.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com
my qq num is .
not #匹配所有
[root@web02 ]# grep "." test.txt
I am timfly student!
I learn linux is very happy.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com
my qq num is .
not #匹配单个任意字符
[root@web02 ]# grep "tim.ly" test.txt
I am timfly student!
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com #匹配以.结尾的
[root@web02 ]# grep "\.$" test.txt
I learn linux is very happy.
my qq num is .
not . #精确匹配到
[root@web02 ]# grep -o "8*" test.txt [root@web02 ]# grep -o "tim*" test.txt
tim
tim
tim #匹配有abc的行,数字所在的行,所有小写字母
[root@web02 ]# grep "[abc]" test.txt
I am timfly student!
I learn linux is very happy.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com [root@web02 ]# grep "[0-9]" test.txt
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com
my qq num is .
not . [root@web02 ]# grep "[a-z]" test.txt
I am timfly student!
I learn linux is very happy.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com
my qq num is .
not . [root@web02 ]# grep "[a-z]" test.txt
I am timfly student!
I learn linux is very happy.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http://timfly.blog.51cto.com
our site is http://www.timfly123.com
my qq num is .
not . #重复8三次
[root@web02 ]# grep "8\{3\}" test.txt ##带转义字符
not .
[root@web02 ]# grep "8{3}" test.txt ##不带转义字符也没有正则
[root@web02 ]# egrep "8{3}" test.txt ##正则匹配
not .
[root@web02 ]# grep -E "8{3}" test.txt ##加-E 参数
not . #重复数字8,-5次
[root@web02 ]# egrep "8{3,5}" test.txt
not . #重复8一次或以上
[root@web02 ]# grep -E "8{1,}" test.txt
not .
正则表达式基础->的更多相关文章
- .NET正则表达式基础入门
这是我第一次写的博客,个人觉得十分不容易.以前看别人写的博客文字十分流畅,到自己来写却发现十分困难,还是感谢那些为技术而奉献自己力量的人吧. 本教程编写之前,博主阅读了<正则指引>这本入门 ...
- 正则表达式基础---转自 Python正则表达式指南 前边
1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...
- bash shell学习-正则表达式基础 (笔记)
A gentleman is open-minded and optimistic; a small person is narrow-minded and pessimistic. "君子 ...
- python正则表达式基础篇
1.正则表达式基础 1.1简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十分强大 ...
- (大数据工程师学习路径)第一步 Linux 基础入门----正则表达式基础
介绍 虽然我们这一节的标题是正则表达式,但实际这一节只是介绍grep,sed,awk这三个命令,而正则表达式作为这三个命令的一种使用方式(命令输出中可以包含正则表达式).正则表达式本身的内容很多,要把 ...
- 前端学PHP之正则表达式基础语法
前面的话 正则表达式是用于描述字符排列和匹配模式的一种语法规则.它主要用于字符串的模式分割.匹配.查找及替换操作.在PHP中,正则表达式一般是由正规字符和一些特殊字符(类似于通配符)联合构成的一个文本 ...
- python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别
正则表达式(regular expression)是一个特殊的字符序列,描述了一种字符串匹配的模式,可以用来检查一个字符串是否含有某种子字符串. 将匹配的子字符串替换或者从某个字符串中取出符合某个条件 ...
- Linux学习(2)- 正则表达式基础
Linux学习(2)- 正则表达式基础 一.基础正则表达式介绍与练习 学习内容 正则表达式特殊符号 [:alnum:]代表英文大小写字母及数字 [:alpha:]代表英文大小写字母 [:blank:] ...
- javascript之正则表达式基础知识小结
javascript之正则表达式基础知识小结,对于学习正则表达式的朋友是个不错的基础入门资料. 元字符 ^ $ . * + ? = ! : | \ / ( ) [ ] { } 在使用这些符号时需要 ...
- Python正则表达式-基础
Python正则表达式-基础 本文转载自昔日暖阳,原文地址:http://www.osheep.cn/4806.html python使用正则,需要先引入re模块 import re 匹配符 单个字符 ...
随机推荐
- BZOJ3601 一个人的数论 【数论 + 高斯消元】
题目链接 BZOJ3601 题解 挺神的 首先有 \[ \begin{aligned} f(n) &= \sum\limits_{x = 1}^{n} x^{d} [(x,n) = 1] \\ ...
- poi导出word表格详解 超详细了
转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138 一.效果如下 二.js代码 function export_word( ...
- Python完成RF测试用例
Robot Framework 框架是基于 Python 语言开发的,所以,它本质上是 Python 的一个库. from robot.api import TestSuite from robot. ...
- Chapter4(表达式) --C++Prime笔记
1.重载运算符:为已经存在的运算符赋予另外一层含义. 2.左值与右值: ①当一个对象被用作右值的时候,用的是对象的值(内容):当一个对象被用作左值的时候,用的是对象的身份(在内存中的位置). ②在 ...
- tp 事务处理
tp的事务开启是非常简单的, 只需要M()->startTrans();//开启事务,M()可以是M('xxx') $m->rollback();//事务回滚 $m->commit( ...
- P1792 [国家集训队]种树
P1792 [国家集训队]种树 题目描述 A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树. 园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n.并且每 ...
- nova-api中ExtensionManager的构造
源码版本:H版 nova/api/openstack/__init__.py APIRouter类: def __init__(self, ext_mgr=None, init_only=None): ...
- Object-C使用类静态方法创建对象时容易内存泄露
1.所谓使用类的静态方法创建对象,就是指使用类名调用一次它的静态方法(非显式调用alloc)便可以得到一个新建的对象,比如下面两个例子: NSString* str1 = [NSString stri ...
- python---函数补充(变量传递),语句执行顺序(入栈顺序)
一:函数补充 默认作为函数参数的数据,是浅拷贝传递.不是和C等语言一样,产生一个临时变量. class T: def __init__(self,num): print(id(num)) self.n ...
- ASP.NET Core的身份认证框架IdentityServer4--(5)自定义用户登录(使用官网提供的UI)
IdentityServer官方提供web页面,可以根据需求修改样式.具体UI下载跟配置参考官网文档. 文档地址:https://identityserver4.readthedocs.io/en/r ...