【开发者成长】5 分钟搞定 Linux 正则表达式
云栖号资讯:【点击查看更多行业资讯】
在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来!
正则表达式是一种字符模式,用于在查找过程中匹配制定的字符。
元字符通常在Linux中分为两类:
- Shell元字符,由Linux Shell进行解析;
- 正则表达式元字符,由vi/grep/sed/awk等文本处理工具进行解析;
正则表达式一般以文本行进行处理,在进行下面实例之前,先为grep命令设置—color参数:
$ alias grep='grep --color=auto'
这样每次过滤出来的字符串都会带色彩了。
在开始之前还需要做一件事情,就是创建一个测试用的re-file文件,内容如下:
$ cat re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
正则表达式元字符
特殊的元字符
扩展的正则表达式
实操
匹配以love开头的所有行
$ grep '^love' re-file
love, how much I adore you. Do you know
匹配love结尾的所有行
$ grep 'love$' re-file
clover. Did you see them? I can only hope love.
匹配以l开头,中间包含两个字符,结尾是e的所有行
$ grep 'l..e' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
匹配0个或多个空行,后面是love的字符
$ grep ' *love' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
匹配love或Love
$ grep '[Ll]ove' re-file # 对l不区分大小写
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
匹配A-Z的字母,其次是ove
$ grep '[A-Z]ove' re-file
Lovers were all around us. It is springtime. Oh
匹配不在A-Z范围内的任何字符行,所有的小写字符
$ grep '[^A-Z]' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
匹配love.
$ grep 'love\.' re-file
clover. Did you see them? I can only hope love.
匹配空格
$ grep '^$' re-file
匹配任意字符
$ grep '.*' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
前面o字符重复2到4次
$ grep 'o\{2,4\}' re-file
groove.
重复o字符至少2次
$ grep 'o\{2,\}' re-file
groove.
重复0字符最多2次
$ grep 'o\{,2\}' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
重复前一个字符一个或一个以
$ egrep "go+d" linux.txt
Linux is a good
god assdxw bcvnbvbjk
gooodfs awrerdxxhkl
good
0个或者一个字符
ansheng@Ubuntu:/tmp$ egrep "go?d" linux.txt
god assdxw bcvnbvbjk
gdsystem awxxxx
或,查找多个字符串
$ egrep "gd|good" linux.txt
Linux is a good
gdsystem awxxxx
good
分组过滤匹配
$ egrep "g(la|oo)d" linux.txt
Linux is a good
glad
good
【云栖号在线课堂】每天都有产品技术专家分享!
课程地址:https://yqh.aliyun.com/zhibo立即加入社群,与专家面对面,及时了解课程最新动态!
【云栖号在线课堂 社群】https://c.tb.cn/F3.Z8gvnK
原文链接
本文为云栖社区原创内容,未经允许不得转载。
【开发者成长】5 分钟搞定 Linux 正则表达式相关推荐
- 9月29日云栖精选夜读 | 17个案例带你3分钟搞定Linux正则表达式
正则表达式是一种字符模式,用于在查找过程中匹配制定的字符. 元字符通常在Linux中分为两类: Shell元字符,由Linux Shell进行解析: 正则表达式元字符,由vi/grep/sed/awk ...
- linux正则表达式_【开发者成长】5 分钟搞定 Linux 正则表达式
云栖号资讯:[点击查看更多行业资讯] 在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来! 正则表达式是一种字符模式,用于在查找过程中匹配制定的字符. 元字符通常在Linux中分为两类: S ...
- 15分钟搞定Linux三剑客 grep sed awk
剑客grep 来者何人 grep是linux上的字符串搜索匹配命令.这个名字来自于linux编辑器vim的一个命令g/re/p,表示全局匹配某个正则表达式并打印出来,这也体现了grep自身的功能. 基 ...
- linux grep 匹配空格_17 个案例,5 分钟简单搞定 Linux 正则表达式!
正则表达式是一种字符模式,用于在查找过程中匹配制定的字符. 元字符通常在Linux中分为两类: Shell元字符,由Linux Shell进行解析: 正则表达式元字符,由vi/grep/sed/awk ...
- 五分钟搞定 Linux 文档全部知识,就看这篇文章
作者:无痴迷,不成功 来源:见文末 写在前面 我们都知道Linux是一个支持多用户.多任务的系统,这也是它最优秀的特性,即可能同时有很多人都在系统上进行工作,所以千万不要强制关机,同时,为了保护每个人 ...
- 正则表达式里转义字符_五分钟搞定正则表达式,如果没搞定,再加两分钟
五分钟搞定正则表达式,如果没搞定,再加两分钟 [这是 ZY 第 18 篇原创文章] 文章概览 一.正则表达式介绍 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简 ...
- linux shell find depth,搞定 Linux Shell 文本处理工具,看完这篇集锦就够了
原标题:搞定 Linux Shell 文本处理工具,看完这篇集锦就够了 Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕 ...
- 视频教程-10分钟搞定 php+H5手机网页微信支付 在线视频教程(含源代码)-微信开发
10分钟搞定 php+H5手机网页微信支付 在线视频教程(含源代码) 04年进入计算机行业.拥有6年net和php项目开发经验,8年java项目开发经验. 现前端全栈工程师,主攻产品设计,微信开发等. ...
- 3 分钟搞定 Android Push
在 Android 上,要实现 Push 功能可没有那么简单! 众所周知的原因是,Android 官方的推送服务 GCM 在国内手机上用不了.所以很多国内的开发者,不得不去使用 AndroidPN 这 ...
最新文章
- java 二进制模块_深入Node模块Buffer-学会操作二进制
- UNIX下C语言的图形编程-curses.h函数库
- 藁城一中2021年高考成绩查询,2017藁城一中录取分数线及高考成绩情况
- 简单Hook SYSENTER
- excel 电阻并联计算_电阻器的构成及取代原则
- [vijos1162]波浪数
- raid 物理盘缓存状态_CDN与其他层面缓存
- neo4j values
- commons-fileupload实现单次上传文件(word文档)
- 1027. 打印沙漏(20)-PAT乙级真题
- 解决请求筛选模块被配置为拒绝包含的查询字符串过长的请求
- mysql数据库语法——(六)
- StackPanel:栈式面板基础简述
- HBase 权威指南笔记
- 每日作业-品优购详情页
- 如何选择和设置SEO关键词
- OSChina 周一乱弹 ——杜牧你个老流氓!
- 安全牛联合世平信息共同发布《数据防泄密 (DLP) 业务应用指南》
- Excel应用技巧:合并单元格的排序
- 用示波器实现跳舞视频
热门文章
- python mysql 保存csv_使用Python将csv文件快速转存到Mysql
- 成为一名成功的程序员要做到以下10点?网友:是真的吗?
- 什么意思_invalid是什么意思
- 【LeetCode笔记】309. 最佳买卖股票时机含冷冻期(Java、动态规划)
- 【学习笔记】传输层:TCP协议(报文段、连接管理{握手}、可靠传输、流量控制、拥塞控制)
- simulink和c语言开发,Simulink之嵌入式C代码生成-应用层和底层的接口
- java xca碗组口径,公路车常见杂音检查清单和解决方法(图文)
- cplex安装_Excel软件规划求解工具的安装与功能介绍
- roadhog不是内部或外部命令_git:git 不是内部或外部命令,也不是可运行的程序
- 一英寸芯片大小_科普:为什么标称1英寸的CMOS成像芯片,其对角线长度不是25.4mm?...