8.4 反后顾

最后一种环视是反后顾。你能猜到它的工作原理吗?

反后顾会查看某个模式在从左至右的文本流的后面没有出现。同样,它有一个小于号(<),提醒后顾的是哪个方向。

请在RegExr中使用这个命令并查看结果:

  1. (?1)(?<!ancyent) marinere

滚动到下方看看结果是什么。

然后用Perl语言尝试:

  1. perl -ne 'print if /(?i)(?<!ancyent) marinere/' rime.txt

以下就是你会看到的结果,没有一个地方含有单词ancyent:

  1. The Marinere hath his will.
  2. The bright-eyed Marinere.
  3. The bright-eyed Marinere.
  4. The Marineres gave it biscuit-worms,
  5. Came to the Marinere's hollo!
  6. Came to the Marinere's hollo!
  7. The Marineres all 'gan work the ropes,
  8. The Marineres all return'd to work
  9. The Marineres all 'gan pull the ropes,
  10. "When the Marinere's trance is abated."
  11. He loves to talk with Marineres
  12. The Marinere, whose eye is bright,

最后在ack中这样做:

  1. ack -i '(?<!ancyent) marinere' rime.txt

以上就是对前瞻和后顾的简单介绍,这是现代正则表达式的一个重要特性。

在下一章中,你将会看到如何使用sed和Perl为文档添加HTML5标签的完整例子。