为了账号安全,请及时绑定邮箱和手机立即绑定

正则表达式 lookaround 的一个问题。

正则表达式 lookaround 的一个问题。

心有法竹 2018-08-22 10:09:01
问题是关于 Positive 和 Negative LookAhead 的一个模式:匹配的文本为 quite\nqaiet首先第一个模式: q(?=u) 毫无疑问,第一个q 会被匹配到,第二个模式 q(?!u) 毫无疑问,第二个q 会被匹配到接下来疑问来了,请问q(?=u)i 这个模式能匹配到怎么样的文本?
查看完整描述

1 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

可以匹配 qii 这个文本,关于 为什么无法匹配到quite 的原因是:
文本 quite
模式 q(?=u)i

解释如下:

q matches q and u matches u. Again, the match from the lookahead must be discarded, so the engine steps back from i in the string to u. The lookahead was successful, so the engine continues with i. But i cannot match u.

中文解释就是,模式中的q匹配了文本中的q模式中的u匹配了文本中的u,由于这个是 lookahead 模式,所以,下一个匹配还是从文本中的u开始,然而 文本中的 u 并不匹配 模式中的i,所以这不会匹配。

PS:The difference is that lookaround actually matches characters, but then gives up the match, returning only the result: match or no match.


查看完整回答
反对 回复 2018-08-26
  • 1 回答
  • 0 关注
  • 666 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信