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

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


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


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


心有法竹
浏览 675回答 1
1回答

猛跑小猪

可以匹配 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.
打开App,查看更多内容
随时随地看视频慕课网APP