字符匹配
正则表达式的关键之处在于确定你要搜索匹配的东西,如果没有这一概念,Res将毫无用处。
每一个表达式都包含需要查找的指令,如表A所示。
Table A: Character-matching regular expressions |
操作 | 解释 | 例子 | 结果 |
. | Match any one character | grep .ord sample.txt | Will match “ford”, “lord”, “2ord”, etc. in the file sample.txt. |
[ ] | Match any one character listed between the brackets | grep [cng]ord sample.txt | Will match only “cord”, “nord”, and “gord” |
[^ ] | Match any one character not listed between the brackets | grep [^cn]ord sample.txt | Will match “lord”, “2ord”, etc. but not “cord” or “nord” |
| | grep [a-zA-Z]ord sample.txt | Will match “aord”, “bord”, “Aord”, “Bord”, etc. |
| | grep [^0-9]ord sample.txt | Will match “Aord”, “aord”, etc. but not “2ord”, etc. |
重复操作符
重复操作符,或数量词,都描述了查找一个特定字符的次数。它们常被用于字符匹配语法以查找多行的字符,可参见表B。
Table B: Regular expression repetition operators |
操作 | 解释 | 例子 | 结果 |
? | Match any character one time, if it exists | egrep “?erd” sample.txt | Will match “berd”, “herd”, etc. and “erd” |
* | Match declared element multiple times, if it exists | egrep “n.*rd” sample.txt | Will match “nerd”, “nrd”, “neard”, etc. |
+ | Match declared element one or more times | egrep “[n]+erd” sample.txt | Will match “nerd”, “nnerd”, etc., but not “erd” |
{n} | Match declared element exactly n times | egrep “[a-z]{2}erd” sample.txt | Will match “cherd”, “blerd”, etc. but not “nerd”, “erd”, “buzzerd”, etc. |
{n,} | Match declared element at least n times | egrep “.{2,}erd” sample.txt | Will match “cherd” and “buzzerd”, but not “nerd” |
{n,N} | Match declared element at least n times, but not more than N times | egrep “n[e]{1,2}rd” sample.txt | Will match “nerd” and “neerd” |
锚
锚是指它所要匹配的格式,如图C所示。使用它能方便你查找通用字符的合并。例如,我用vi行编辑器命令:s来代表substitute,这一命令的基本语法是:
s/pattern_to_match/pattern_to_substitute/
Table C: Regular expression anchors |
操作 | 解释 | 例子 | 结果 |
^ | Match at the beginning of a line | s/^/blah / | Inserts “blah “ at the beginning of the line |
$ | Match at the end of a line | s/$/ blah/ | Inserts “ blah” at the end of the line |
\< | Match at the beginning of a word | s/\</blah/ | Inserts “blah” at the beginning of the word |
| | egrep “\<blah” sample.txt | Matches “blahfield”, etc. |
\> | Match at the end of a word | s/\>/blah/ | Inserts “blah” at the end of the word |
| | egrep “\>blah” sample.txt | Matches “soupblah”, etc. |
\b | Match at the beginning or end of a word | egrep “\bblah” sample.txt | Matches “blahcake” and “countblah” |
\B | Match in the middle of a word | egrep “\Bblah” sample.txt | Matches “sublahper”, etc. |
间隔
Res中的另一可便之处是间隔(或插入)符号。实际上,这一符号相当于一个OR语句并代表|符号。下面的语句返回文件sample.txt中的“nerd” 和 “merd”的句柄:
egrep “(n|m)erd” sample.txt
间隔功能非常强大,特别是当你寻找文件不同拼写的时候,但你可以在下面的例子得到相同的结果:
egrep “[nm]erd” sample.txt
当你使用间隔功能与Res的高级特性连接在一起时,它的真正用处更能体现出来。
一些保留字符
Res的最后一个最重要特性是保留字符(也称特定字符)。例如,如果你想要查找“ne*rd”和“ni*rd”的字符,格式匹配语句“n[ei]*rd”与“neeeeerd” 和 “nieieierd”相符合,但并不是你要查找的字符。因为‘*’(星号)是个保留字符,你必须用一个反斜线符号来替代它,即:“n[ei]\*rd”。其它的保留字符包括:
- ^ (carat)
- . (period)
- [ (left bracket}
- $ (dollar sign)
- ( (left parenthesis)
- ) (right parenthesis)
- | (pipe)
- * (asterisk)
- + (plus symbol)
- ? (question mark)
- { (left curly bracket, or left brace)
- \ backslash
一旦你把以上这些字符包括在你的字符搜索中,毫无疑问Res变得非常的难读。比如说以下的PHP中的eregi搜索引擎代码就很难读了。
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$",$sendto)
你可以看到,程序的意图很难把握。但如果你抛开保留字符,你常常会错误地理解代码的意思。如果你想阅览Open Group组织的规则的完整描述,你可以参见:Regular Expressions,欢迎你在其中的讨论区发表你的问题或观点。
分享到:
相关推荐
如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么简单。如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符构成的串,它定义了一...
正则表达式,正则表达式,正则表达式 正则表达式 正则表达式 正则表达式 正则表达式 正则表达式 正则表达式
常用正则表达式 正则表达式 常用正则表达式 正则表达式
正则表达式整理汇总,含以下内容 [1]里面在包括一个正则表达式测试小程序 RegexTester.exe [2]正则表达式30分钟入门教程.pdf] [3]正则表达式规则.doc [4]正则表达式30分钟入门教程.mht 自己整理用的,现在...
正则表达式.rar正则表达式.rar正则表达式.rar正则表达式.rar
正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式...
《学习正则表达式》从正则表达式的基本概念讲起,到编写完整的sed和Perl脚本,再到转换HTML文件,将这种强大的工具解释得清晰透彻。...《学习正则表达式》适合对正则表达式感兴趣的程序员和互联网从业者。
正则表达式验证工具 V1.0 本软件主要用于检测正则表达式是否正确。 运行环境:本软件为绿色软件,无需安装,但需要Microsoft .NET Framework 4 支持,如果没有请前去下载(下载路径:...
正则表达式正则表达式正则表达式正则表达式 验证程序
正则表达式日期校验 正则表达式日期校验 正则表达式日期校验
Java正则表达式Java正则表达式Java正则表达式Java正则表达式
正则表达式帮助 有用的正则表达式帮助 有用的正则表达式帮助 有用的正则表达式帮助 有用的
正则表达式 正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式
《正则表达式经典实例》讲解了基于8种常用的编程语言使用正则表达式的经典实例。书中提供了上百种可以在实战中使用的实例,以帮助读者使用正则表达式来处理数据和文本。对于如何使用正则表达式来解决性能不佳、误报...
正则表达式 详解 基础 正则表达式之道(A Tao of Regular Expressions)
正则表达式总结正则表达式总结正则表达式总结正则表达式总结正则表达式总结正则表达式总结
正则表达式速查 正则表达式举例 正则表达式学习 (4页A4纸)
常用正则表达式大全.txt常用正则表达式大全.txt常用正则表达式大全.txt常用正则表达式大全.txt常用正则表达式大全.txt
UE正则表达式、UE正则表达式
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。...正则表达式通常缩写成"regex",单数有regexp、regex,复数有regexps、regexes、regexen。