Regex 具有负前瞻性的正则表达式仍然匹配
我正在测试一个shebang“#!”,看看它的两边是否都有一个正斜杠和这个正则表达式Regex 具有负前瞻性的正则表达式仍然匹配,regex,Regex,我正在测试一个shebang“#!”,看看它的两边是否都有一个正斜杠和这个正则表达式(?!\/)#\!(?!\/) 它应该匹配: #,z#,#!z,z#!z 它应该不匹配:/#,#/,/#/ 我在shebang周围放置了负数lookahead,这样它就不会匹配任何斜杠,实际上它也不会匹配尾随斜杠,但出于某种原因,它仍然匹配前导斜杠/# 关于为什么会发生这种情况/如何解决它,您有什么想法吗?您想结合使用 (? 说明: (?<! # look behind to see if the
(?!\/)#\!(?!\/)
它应该匹配:
#代码>,z#代码>,#!z
,z#!z
它应该不匹配:/#代码>,#/代码>,/#/代码>
我在shebang周围放置了负数lookahead,这样它就不会匹配任何斜杠,实际上它也不会匹配尾随斜杠,但出于某种原因,它仍然匹配前导斜杠/#代码>
关于为什么会发生这种情况/如何解决它,您有什么想法吗?您想结合使用
(?
说明:
(?<! # look behind to see if there is not:
/ # '/'
) # end of look-behind
#! # '#!'
(?! # look ahead to see if there is not:
/ # '/'
) # end of look-ahead
(?您的第一个先行应该是一个后向。
(?<! # look behind to see if there is not:
/ # '/'
) # end of look-behind
#! # '#!'
(?! # look ahead to see if there is not:
/ # '/'
) # end of look-ahead