1、向前查找
向前查找被匹配的文本不包含在最终返回的匹配结果里,这被称为“不消费”。
向前查找的模式就是一个子表达式,一个以?=开头的子表达式。需要匹配的文本跟在=后面。
http://www.forta.com/ https://mail.forta.com/ ftp://ftp.forta.com/ 正则表达式:.+(?=:) 匹配结果:共找到3处匹配 http https ftp
子表达式(?=:)匹配:,注意被匹配的:并没有出现在最终的匹配结果里,我们用?=向正则表达式引擎表明:只要找到就行了,不要把它包括在最终的匹配结果里。用术语讲就是不消费它。
正则表达式:.+(:)
匹配结果:共找到3处匹配 http:
https:
ftp:
子表达式(:)正确的匹配到了:并消费了这个字符。:出现在最后的匹配结果里。
注意:任何一个子表达式都可以转换为一个向前查找表达式,只要给它加上一个?=前缀即可。在同一个搜索模式里可以使用多个向前查找表达式,它们可以出现在模式里的任意位置(而不仅仅是出现在整个模式的开头,就像你们在上面看到的那样)。