正则表达式基础->
描述:(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 匹配符 单个字符 ...
随机推荐
- 【刷题】BZOJ 5415 [Noi2018]归程
www.lydsy.com/JudgeOnline/upload/noi2018day1.pdf Solution 考试的时候打的可持久化并查集,没调出来QAQ 后面知道了kruskal重构树这个东西 ...
- 学习Spring Boot:(四)应用日志
前言 应用日志是一个系统非常重要的一部分,后来不管是开发还是线上,日志都起到至关重要的作用.这次使用的是 Logback 日志框架. 正文 Spring Boot在所有内部日志中使用Commons L ...
- WEB入门二 表格和表单
学习内容 Ø 表格的作用和制作 Ø 表单的制作 能力目标 Ø 掌握表格的创建 Ø 掌握设置表格的常用属性: Ø 理解表单的作用 Ø ...
- 遇到问题----java----myeclipse或者eclipse发布的项目时配置文件不更新或者无配置文件
myeclipse或者eclipse发布的项目时配置文件不更新或者无配置文件. 正常的web项目有目录 src/main/resources 和 src/main/java 这两个目录默认在编译发布时 ...
- 常用Build-in Keywords
1. Variables |- Set variable |- Create list |- Evaluate |- Get Variable Value 2. Conditional |- Run ...
- struts的问题
将SSH框架进行整合的时候,将三者的jar包加入到lib下面,然后测试struts,结果页面显示不出来报404错误,可是路径没有问题 找到罪魁祸首是:原因两个:(1)在未用到spring的时候,先不要 ...
- No module named 'urllib.request'; 'urllib' is not a package
想学爬虫urllib的设置代理服务器,于是把之前跳过没学的urllib捡起来,敲了段简单的代码,如下 import urllib.request url = "http://www.baid ...
- html5 +css3 点击后水波纹扩散效果 兼容移动端
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Hi35xx 通用GPIO 使用篇(板子3G电源控制脚说明)
在一个嵌入式系统中使用最多的莫过于 通用输入输出 GPIO口.看到论坛中经常有朋友问海思为什么没有提供GPIO驱动.其实不然. 在海思SDK xxx/osdrv/tools/board_tools/ ...
- bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...