正则表达式基础->
2024-09-24 14:26:55
描述:(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 .
最新文章
- 在DigitalOcean云主机上搭建SVN服务器
- 在嵌入式开发板中运行程序提示-/bin/sh: ./xx: not found的解决办法
- iOS设计模式 - 单例
- null&;this&;super&;向上转型
- Hadoop学习2--Linux准备及环境准备
- Good Bye 2015 A. New Year and Days 签到
- Parallel类(简化Task 操作)
- Spring Security(19)——对Acl的支持
- Laravel的ORM入门
- 1787: [Ahoi2008]Meet 紧急集合
- 如何用webpack实现自动化的前端构建工作流
- 《Create Your own PHP Framework》笔记
- 索信达携手8Manage,打造项目管理系统信息化体系
- Android ROM开发(一)——Windows下Cygwin和Android_Kitchen厨房的安装
- RocketMQ4.3.x对顺序消息的理解
- socket学习
- python second lesson
- lombok插件:Data自动get/set方法, Slf4j实现Logger的调用
- ecstore-自定义app里增加对应的wap的模板
- Java 生成ZIP文件
热门文章
- mac 使用tree命令
- 笛卡尔树Cartesian Tree
- Python之旅:列表
- 【OpenCV】角点检测:Harris角点及Shi-Tomasi角点检测
- bzoj1874 [BeiJing2009 WinterCamp]取石子游戏
- nltk30_Investigating bias with NLTK
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- Maven项目导出jar包配置
- linux netstat查看服务和端口状态
- springcloud入门系列(二):注册中心Eureka