Linux学习之正则表达式



Linux学习之正则表达式
 
正则表达式字符串表示方式一朝不同的严谨程度分为基础正则表达式和扩展正则表达式,
下面我们来先看看基础正则表达式:
经常用到的一些符号
 
[;alnum:]  表示0-9  a-z   A-Z
[:alpha:]  表示a-z  A-Z
[:lower:] 表示a-z
[:upper:] 表示A-Z
[:digit:] 表示0-9
   www.2cto.com  
在学习正则表达式之前有必要好好了解grep的使用
 
grep [-A] [-B] [--color=auto] '查找字符串' filename
A  后面可以接数字,为after的意思,除了列出该行外,后续的n行业列出来
B 后面可以加数字,为before的意思,除了列出改行外,前面的n行业列出来
 
 
现在给一段练习正则表达式的英文段:
"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.^M
GNU is free air not free beer.^M
Her hair is very beauty.^M
I can't finish the test.^M
Oh! The soup taste good.^M
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!     My god!  www.2cto.com  
The gd software is a library for drafting programs.^M
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
(从鸟哥私房菜下载)
这个文件共有22行,最下面一行为空白行,现在开始一个案例一个案例的学习吧
 
案例1 查找含有the的行


 
案例2
查找不含有the的行

 
案例3 包含'the' 或者'The'的行

 
案例4 查找包含test,taste的行,
仔细分析要求,test,taste中都包含t s,t
  www.2cto.com  
 
案例5  找出oo前面不是g的行

 
案例6  查找包含数字的行

 
案例7  查找以小写字母开头的行


 
案例8 找出行尾结束为小数点的
 

 
案例10 查找空白行
 
 
在正在表达式中
 .   表示一个任意的字符
* 表示前一个字符有重复0或者多次
那么.*就表示任意字符重复任意多次包括0次
 
讲解最后一个知识点  限定连续字符范围{}
案例11 查找有2-5个连续0的行  www.2cto.com  
 

 
 基础正则表达式字符
 
^word:  待查字符串在行首
wrod$  待查字符串在行尾
* 重复0到无穷多个前一个字符
[list] 从字符集和里面找到想要的字符
[^list] 从集合的字符里面找出不要的字符串范围
\{m,n\} 连续m 到n个前一个字符
 
 
作者 yuanzeyao2008

相关内容

    暂无相关文章