首页 新闻 赞助 找找看

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

0
[已解决问题] 解决于 2018-02-28 17:07

问题是关于 Positive 和 Negative LookAhead 的一个模式:匹配的文本为 quite\nqaiet


首先第一个模式: q(?=u) 毫无疑问,第一个q 会被匹配到,
第二个模式 q(?!u) 毫无疑问,第二个q 会被匹配到


接下来疑问来了,请问q(?=u)i 这个模式能匹配到怎么样的文本?

BUTTERAPPLE的主页 BUTTERAPPLE | 老鸟四级 | 园豆:3190
提问于:2017-12-05 14:50
< >
分享
最佳答案
0

可以匹配 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.

BUTTERAPPLE | 老鸟四级 |园豆:3190 | 2017-12-05 18:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册