环视1、正前瞻2、反前瞻3、正后顾4、反后顾待匹配文本THERIMEOFTHEANCYENTMARINERE,INSEVENPARTS.ARGUMENT.How a Ship having passed the Line was driven by Storms to the cold Country towards the South Pole;and how from thence she made her course to the tropical Latitudeofthe Great Pacific Ocean;andofthe strange things that befell;andinwhat manner the Ancyent Marinere came back to his own Country.I.It is an ancyent Marinere,And he stoppeth oneofthree:By thy long grey beard and thy glittering eye Now wherefore stoppest me?1、正前瞻假设要匹配单词ancyent且要求紧随其后的单词是marinere为与文件中的单词一致我采用了古代拼写法。要达到这个目的我们可以使用正前瞻。首先在RegExr桌面版程序中试一下。将下面这个不区分大小写的模式输入到顶部的文本框内(?i)ancyent(?marinere)由于我们使用了不区分大小写的选项? i就不必担心模式中用的大小写形式了。现在就是在每一行中寻找后跟marinere的单词ancyent。结果会在模式区域下面的文本区中标亮但只有模式的第一部分ancyent是被标亮的环视模式Marinere不会标亮。现在使用Perl来做正前瞻。你可以写这样一个命令现在使用Perl来做正前瞻。你可以写这样一个命令perl-neprint if /(?i)ancyent(? marinere)/rime2.txt其输出如下THERIMEOFTHEANCYENTMARINERE,INSEVENPARTS.How a Ship having passed the Line was driven by Storms to the cold Country towards the South Pole;and how from thence she made her course to the tropical Latitudeofthe Great Pacific Ocean;andofthe strange things that befell;andinwhat manner the Ancyent Marinere came back to his own Country.It is an ancyent Marinere,God save thee,ancyent Marinere!Ifear thee,ancyent Marinere!该诗有五行内容中的ancyent出现在marinere之前。如果要求ancyent之后的单词以大写或小写字母m开头该如何做呢可以这样perl-neprint if /(?i)ancyent (? m)/rime.txt现在除了Marinere跟在后面的还可以有man和ManAnd thus spake on that ancyent man, And thus spake on that ancyent Man,ack也可以使用环视功能这是因为它是由Perl语言编写的。ack的命令行界面与grep十分相似。试一下这个命令ack(? i)ancyent (? ma)rime.txt试一下这个命令ack(? i)ancyent (? ma)rime.txt你会看到高亮显示的结果如图所示。使用ack时可以用命令行选项-i指定不区分大小写而不使用嵌入选项(? i)ack-iancyent (? ma)rime.txt使用ack时可以用命令行选项-i指定不区分大小写而不使用嵌入选项(? i)ack-iancyent (? ma)rime.txt若要在ack的输出中添加行号以方便计数可以采用多种方法。比如可以加上-H选项ack-Hiancyent (? ma)rime.txt也可以添加带有–output选项的代码ack-i--output$.:$_ancyent (? ma)rime.txt这有点不太正统——关闭了标亮功能但它确实起作用了。2、反前瞻反前瞻是对正前瞻的取反操作。这意味着要匹配某个模式时需要在它后面找不到含有给定前瞻模式的内容。反前瞻的形式是(? i)ancyent(?!marinere)只有一个字符发生了变化正前瞻的等号变为反前瞻的感叹号!。下图所示为在Opera中执行反前瞻查找。在Perl语言中可以这样来使用反前瞻perl-neprint if /(? i)ancyent (? ! marinere)/rime.txt而我们会得到的结果是And thus spake on that ancyent man, And thus spake on that ancyent Man,在ack中可以用ack-iancyent (? ! marinere)rime.txt得到相同结果。3、正后顾正后顾会查看左边的内容这与正前瞻方向相反。其语法是(?i)(?ancyent)marinere正后顾使用小于号提醒你后顾是哪个方向。请在RegExr中试一下看看有什么不同。被标亮的是marinere而不是ancyent。为什么因为正后顾的模式是匹配的条件不会包含在匹配结果中。在Perl语言中这样做perl-neprint if /(? i)(? ancyent) marinere/rime.txt在ack中这样做ack-i(? ancyent) marinererime.txt4、反后顾最后一种环视是反后顾。你能猜到它的工作原理吗反后顾会查看某个模式在从左至右的文本流的后面没有出现。同样它有一个小于号提醒后顾的是哪个方向。请在RegExr中使用这个命令并查看结果(?1)(?!ancyent)marinere滚动到下方看看结果是什么。然后用Perl语言尝试perl-neprint if /(? i)(? ! ancyent) marinere/rime.txt以下就是你会看到的结果没有一个地方含有单词ancyentThe Marinere hath his will. The bright-eyed Marinere. The bright-eyed Marinere. The Marineres gave it biscuit-worms, Came to the Marineres hollo! Came to the Marineres hollo!The Marineres allgan work the ropes, The Marineres all returnd to work The Marineres allgan pull the ropes, When the Marineres trance is abated. He loves to talk with Marineres The Marinere, whose eye is bright,最后在ack中这样做ack-i(? ! ancyent) marinererime.txt以上就是对前瞻和后顾的简单介绍这是现代正则表达式的一个重要特性。