正则表达式学习 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]\- 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值