匹配此环视正则表达式的字符串

String matching this lookaround regex

我正在学习 this 教程中的正则表达式环视。

它有一个示例解释了如何使用环视检查是否存在(或不存在),但环视括号内的正则表达式并未用于实际匹配。

例如,根据字符串 quit 检查模式 q(?=u)i。而且它 return 不匹配。

例子我看懂了。
但我想不出任何匹配此正则表达式模式的字符串。如果我对环顾四周的理解是正确的,我认为没有任何字符串与此正则表达式匹配。

我说的对吗?如果不是,哪个字符串匹配这个正则表达式?

我不是这个教程网站的忠实粉丝,但如果你仔细阅读它的实际内容,它从未声称正则表达式 q(?=u)i 匹配字符串 quit:

Let's take one more look inside, to make sure you understand the implications of the lookahead. Let's apply q(?=u)i to quit. The lookahead is now positive and is followed by another token. Again, 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. So this match attempt fails. All remaining attempts fail as well, because there are no more q's in the string.

我想您可能仍然对前瞻的工作原理感到困惑。要么,要么你误读了教程网站。如果是前者,则先行通过 断言 匹配来工作,而实际上 消耗 字符串中的任何内容。所以正则表达式 q(?=u)i 表示:

match the letter 'q'
lookahead to the next character after 'q' and assert that it is 'u'
then match an 'i' immediately after the 'q'

当然,字符串'quit'失败了,事实上所有的字符串都会失败。前瞻表示要验证 q 后跟 u,但以下模式通过坚持认为 i 紧随其后与此相矛盾。