揭开正则表达式的神秘面纱

    正则表达式(REs)通常被错误地认为是只有少数人理解的一种神秘语言。在表面上它们确实看起来杂乱无章,如果你不知道它的语法,那么它的代码在你眼里只是一堆文字垃圾而已。实际上,正则表达式是非常简单并且可以被理解。读完这篇文章后,你将会通晓正则表达式的通用语法。

 

支持多种平台

    正则表达式最早是由数学家Stephen Kleene1956年提出,他是在对自然语言的递增研究成果的基础上提出来的。具有完整语法的正则表达式使用在字符的格式匹配方面上,后来被应用到熔融信息技术领域。自从那时起,正则表达式经过几个时期的发展,现在的标准已经被ISO(国际标准组织)批准和被Open Group组织认定。

    正则表达式并非一门专用语言,但它可用于在一个文件或字符里查找和替代文本的一种标准。它具有两种标准:基本的正则表达式(BRE),扩展的正则表达式(ERE)ERE包括BRE功能和另外其它的概念。

    许多程序中都使用了正则表达式,包括xsh,egrep,sed,vi以及在UNIX平台下的程序。它们可以被很多语言采纳,如HTML XML,这些采纳通常只是整个标准的一个子集。

 

比你想象的还要普通

    随着正则表达式移植到交叉平台的程序语言的发展,这的功能也日益完整,使用也逐渐广泛。网络上的搜索引擎使用它,e-mail程序也使用它,即使你不是一个UNIX程序员,你也可以使用规则语言来简化你的程序而缩短你的开发时间。

 

正则表达式101

    很多正则表达式的语法看起来很相似,这是因为你以前你没有研究过它们。通配符是RE的一个结构类型,即重复操作。让我们先看一看ERE标准的最通用的基本语法类型。为了能够提供具有特定用途的范例,我将使用几个不同的程序。

 

字符匹配

    正则表达式的关键之处在于确定你要搜索匹配的东西,如果没有这一概念,Res将毫无用处。

    每一个表达式都包含需要查找的指令,如表A所示。

                   Table A: Character-matching regular expressions

操作    解释        例子                结果

.   Match any one   grep .ord       Will match “ford”,

    character       sample.txt      “lord”, “2ord”,

                                    etc. in the file

                                    sample.txt.

 

[ ] Match any one   grep [cng]ord    Will match only“cord”,

    character       sample.txt      “nord”, and “gord”

    listed between

    the brackets

 

[^ ]Match any one   grep [^cn]ord    Will match “lord”,

    character not   sample.txt      “2ord”, etc.but not

    listed between                  “cord” or “nord”

    the brackets

                    grep [a-zA-Z]ord    Will match “aord”,

                    sample.txt         “bord”,“Aord”,

                                        “Bord”, etc.

 

                    grep [^0-9]ord      Will match “Aord”,

                    sample.txt         “aord”, etc. but

                                        not “2ord”, etc.

 

重复操作符

    重复操作符,或数量词,都描述了查找一个特定字符的次数。它们常被用于字符匹配语法以查找多行的字符,可参见表B

                Table B: Regular expression repetition operators

操作    解释        例子                结果

?   Match any      egrep “?erd”   Will match “berd”, “

    charactere     sample.txt       herd”, etc.and “erd”

    one time,

    if it exists

 

*   Match decl-     egrep“n.*rd”  Will match “nerd”, “

    ared element    sample.txt      nrd”, “neard”, etc.

    multiple tim-

    es, if it

     exists

 

+   Match decl-     egrep “[n]+erd”   Will match “nerd”, “

    ared element    sample.txt          nnerd”,etc., but not

    one or more                       erd”

    times

 

{n} Match decl-     egrep“[a-z]{2}erd”    Will match “cherd”,“

    ared element    sample.txt              blerd”, etc. but not “

    exactly n                              nerd”, “erd”,

    times                                   “buzzerd”,etc.

 

 

{n,}Match decl-     egrep“.{2,}erd”   Will match “cherd”

    ared element    sample.txt           and“buzzerd”, but

    at least n                           not “nerd”

    times

 

{n,N}Match decl-    egrep “n[e]{1,2}rd”      Will match “nerd”

    ared element    sample.txt                and “neerd”

    at least n

    times,but

    not more than

    N times

 

 

    锚是指它所要匹配的格式,如图C所示。使用它能方便你查找通用字符的合并。例如,我用vi行编辑器命令:s来代表substitute,这一命令的基本语法是:

s/pattern_to_match/pattern_to_substitute/

 

                                Table C: Regular expression anchors

 

操作        解释                   例子            结果

 

^   Match at the beginning      /^/blah/    Inserts “blah“

     of a lines                              at the beginning

                                             of the line

 

$   Match at the end of       /$/ blah/       Inserts “blah” at the

     a lines                                 end of the line

 

/<  Match at the beginning    //</blah/      Inserts “blah” at the

    of a words                               beginning of the word

 

                              egrep"/<blah"   Matches “blahfield”

                              sample.txt       ,etc.

 

/>  Match at the end of     //>/blah/        Inserts“blah”

                                             at the end

     a words                                 of the word

                            egrep"/>blah"   Matches “soupblah

                            sample.txt       ”,etc.

 

/b  Match at the beginning   egrep"/bblah"   Matches “blahcake”

     or end of a word        sample.txt      and“countblah”

 

/B  Match in the middle     egrep"/Bblah"   Matches “sublahper”

     of a word               sample.txt     , 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)

 

    你可以看到,程序的意图很难把握。但如果你抛开保留字符,你常常会错误地理解代码的意思。

 

总结

    在本文中,我们揭开了正则表达式的神秘面纱,并列出了ERE标准的通用语法。如果你想阅览Open Group组织的规则的完整描述,你可以参见:Regular Express-ions,欢迎你在其中的讨论区发表你的问题或观点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值