正则表达式学习 2 边界
断言(零宽度断言)标记边界,不匹配字符,用来匹配字符串中的位置。??^和$标记行的起始与结束如 ? ^How.*Country\.$ ? 匹配 ??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 Latitude of the Great Pacific Ocean; and of the strange things that befell; and in what manner the Ancyent Marinere came back to his own Country.?\b 匹配单词边界 ?如 \bTHE\b 匹配?THE?RIME OF?THE?ANCYENT MARINERE, IN SEVEN PARTS.\B 匹配非单词边界如 \Be\B 匹配? ? ? ?It is an ancyent Marinere,?????? And he stoppeth one of three:
? ? ? ?"By thy long grey beard and thy glittering eye
?????? "Now wherefore stoppest me??有些应用中可以用 \< ?\> 来匹配单词边界(很多新式解释器无法使用)(vi vim grep中可以用)不会像\b那样匹配任意单词边界,而是允许分别匹配单词的开头或结尾。?\A \Z可以在Perl和PCRE中匹配主题词的开始和结尾 可以用pcregrep测试(PCRE库的grep版本)如 pcregrep -c '\A\s*(THE|The|the)' rime.txt 可以统计出现在the首附近的空格数(-c选项返回次数)?可以用\Q \E 之间的字符集匹配被误认为是正则符号的字符串如\Q[abc]\E 可以匹配 ? .^$*+?|(){}[abc]\-?