第12章 正则表达式与文件格式化处理

基础正则表达式

语系对正则表达式的影响

不同语系下,字符的编码数据可能不同。

LANG=C:012……ABC……abc……

LANG=zh_CN:012……aAbB……

因此,使用[A-Z]时,搜索到的字符也不一样。

特殊符号 代表意义
[:alnum:] 大小写字符及数字,0-9,A-Z,a-z
[:alpha:] 英文大小写字符
[:blank:] 空格键与tab键
[:cntrl:] 控制按键,CR,LF,TAB,DEL等
[:digit:] 代表数字
[:graph:] 除空格符(空格和Tab)外其他按键
[:lower:] 小写字符
[:print:] 可以被打印出来的字符
[:punct:] 标点字符," ‘ ? ; : # $
[:upper:] 大写字符
[:space:] 任何会产生空白的字符
[:xdigit:] 十六进制数字

grep的一些高级参数

除了上一章介绍的基本用法,grep还有一些高级用法。

grep [-A] [-B] [--color=auto} ‘搜寻字符串‘ filename

参数:

-A:后面可加数字n,为after的意思,除了列出该列,后面的n列也列出来

-B:后面可加数字n,为after的意思,除了列出该列,前面的n列也列出来

--color=auto:对正确选取的数据着色

//-n用于显示行号
[[email protected] 桌面]# dmesg | grep -n --color=auto ‘eth‘
1730:[   10.210383] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:7f:dd:91
1731:[   10.210404] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection

注:grep搜索到字符串后都是以整行为单位显示。

基础正则表达式练习

以下是练习文本

[[email protected] 桌面]# cat regular_express.txt
"Open Source" is a good mechanism to 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!    My god!
The gd software is a library for drafting programs.
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

[[email protected] 桌面]# 

例题一:查找特定字符串

//查找含有the的行
[[email protected] 桌面]# grep -n ‘the‘ regular_express.txt
8:I can‘t finish the test.
12:the symbol ‘*‘ is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.

//查找不含有the的行
[[email protected] 桌面]# grep -vn ‘the‘ regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn‘t fit me.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
11:This window is clear.
13:Oh!    My god!
14:The gd software is a library for drafting programs.
17:I like dog.
19:goooooogle yes!
20:go! go! Let‘s go.
21:# I am VBird
22:
[[email protected] 桌面]# 

例题二:利用中括号[]来查找集合字符

//查找tast或test字符串
[[email protected] 桌面]# grep -n ‘t[ae]st‘ regular_express.txt
8:I can‘t finish the test.
9:Oh! The soup taste good.

//查找不是以g开头的oo字符串
[[email protected] 桌面]# grep -n ‘[^g]oo‘ regular_express.txt
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!

//查找数字
[[email protected] 桌面]# grep -n ‘[0-9]‘ regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.

查找不是以小写字母开头的oo字符串
[[email protected] 桌面]# grep -n ‘[^[:lower:]]oo‘ regular_express.txt
3:Football game is not use feet only.
[[email protected] 桌面]# 

例题三:行首与行尾字符^$

//以the开头的行
[[email protected] 桌面]# grep -n ‘^the‘ regular_express.txt
12:the symbol ‘*‘ is represented as start.

//以小写字母开头的行
[[email protected] 桌面]# grep -n ‘^[a-z]‘ regular_express.txt
2:apple is my favorite food.
4:this dress doesn‘t fit me.
10:motorcycle is cheap than car.
12:the symbol ‘*‘ is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let‘s go.

//以小数点结尾的(需要转义)
[[email protected] 桌面]# grep -n ‘\.$‘ regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn‘t fit me.
10:motorcycle is cheap than car.
11:This window is clear.
12:the symbol ‘*‘ is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
20:go! go! Let‘s go.

//查找空白行
[[email protected] 桌面]# grep -n ‘^$‘ regular_express.txt
22:
[[email protected] 桌面]# 

例题四:任意字符.和重复字符*

.(小数点):代表一定有一个任意字符的意思

*:代表重复前一个0到无穷的意思

//查找以g开头,d结尾,中间两个字符的字符
[[email protected] 桌面]# grep -n ‘g..d‘ regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".

//查找至少含有两个o,后面跟0到无穷个o的字符
[[email protected] 桌面]# grep -n ‘ooo*‘ regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!
[[email protected] 桌面]# 

例题五:限定连续RE字符范围{}

{}必须转义

//查找o重复两次的字符
[[email protected] 桌面]# grep -n ‘o\{2\}‘ regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

//查找o重复2到5次的字符
[[email protected] 桌面]# grep -n ‘o\{2,5\}‘ regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

//查找o重复两次以上的
[[email protected] 桌面]# grep -n ‘go\{2,\}g‘ regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
[[email protected] 桌面]# 

基础正则表达式字符

经过上节的五个例题,可将基础的正则表达式总结如下:

RE字符 意义
^word 带查找的字符串在行首
word$ 待查找的字符串在行尾
. 代表一定有一个任意字符的字符
\ 转义字符
* 重复零到无穷多个前一个字符
[list] 从字符集合的RE字符里找到想要选取的字符
[n1-n2] 从字符集合的RE字符里找到想要选取的字符范围
[^list]
从字符集合的RE字符里找到不想要选取的字符范围
\{n,m\} 前一个字符重复n到m次
时间: 2024-12-25 18:11:35

第12章 正则表达式与文件格式化处理的相关文章

鸟哥的linux私房菜——第12章 正则表达式与文件格式化处理

12.1什么是正则表达式 正则表达式就是处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表达式通过一些特殊符号的辅助,可以让用户轻易达到查找.删除.替换某特定字符串的处理程序. vi.grep.awk.sed支持正则表达式,而cp,ls等命令只能使用bash自身的通配符 12.2基础正则表达式 grep高级参数: grep [-A] [-B] [--color=auto]  'string'   filename -A:after的意思,除了列出该行外,后续的n行也列出来 -B:be

linux正则表达式与文件格式化处理指令

1.特殊符号: 1)[:alnum:]:字母和数字 2)[:alpha:]:字母 3)[:blank:]:空格和tab键 4)[:cntrl:]:控制键CR,LF,Tab,Del等 5)[:digit:]:数字 6)[:graph:]:除了空格符(空格和tab)外的其他按键 7)[:lower:]:小写字母 8)[:print:]:可打印字符 9)[:punct:]:标点符号 10)[:upper:]:大写字母 11)[:space:]:空白字符,如空格,tab,ctr等 12)[:xdigit

12-1-顺序文件归并-文件-第12章-《数据结构》课本源码-严蔚敏吴伟民版

课本源码部分 第12章  文件 - 顺序文件归并 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑        本源码引入的文件  链接? Status.h        相关测试数据下载  链接? 数据包      

文件-第12章-《数据结构题集》习题解析-严蔚敏吴伟民版

习题集解析部分 第12章 文件 ——<数据结构题集>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑       相关测试数据下载  链接? 数据包       本习题文档的存放目录:数据结构\▼配套习题解析\▼12 文件       文

Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化

本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用.概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能. 先定义一个基本的装饰器: ########## 基本装饰器 ########## def orter(func):    #定义装饰器     de

第12章-Swing编程 --- Swing概述

(一)Swing概述 将Swing组件按功能来分: ->顶层容器: JFrame.JApplet.JDialog和JWindow ->中间容器: JPanel.JScrollPane.JSplitPane.JToolBar等 ->特殊容器:在用户界面上具有特殊作用的中间容器,如JInternalFrame.JRootPane.JLayeredPane和JDestopPane等 ->基本组件:实现人机交互的组件,如JButton.JComboBox.JList.JMenu.JSlid

Python装饰器、迭代器&amp;生成器、re正则表达式、字符串格式化

Python装饰器.迭代器&生成器.re正则表达式.字符串格式化 本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用.概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能. 先定义一个基本的装饰器: ########## 基本装饰器 ########

第12章Swing编程

第12章Swing编程 Swing它采用100%java实现 不在依赖本地平台,所有平台都可以保持相同的运行效果,对跨平台支持比较出色  实现了MVC设计模式也称为Model-Delegate(模式-代理) 12.1 Swing概况 独立于本地平台的Swing(速度慢点)组件被称为轻量级组件,而依赖本地平台的AWT组件被称为重量级组件 12.2 Swing 基本组件的用法 12.2.1 Swing 组件层次 大部分Swing组件都是JComponent抽象类的直接或者间接子类,JComponen

12.26&amp;12.27 -正则表达式

12.26&12.27 正则表达式 第1章 使grep/egrep 过滤出的东西加上颜色 cat >>/etc/profile<<EOF alias grep='grep --color=auto' alias egrep='egrep --color=auto' EOF source /etc/profile alias grep egrep 第2章 正则表达式分类 2.1 基础正则表达式:basic    regular expression  BRE  ^  $  .