問題是關于 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.
- 1 回答
- 0 關注
- 750 瀏覽
添加回答
舉報
0/150
提交
取消