Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 PCRE |^/foo/(.*?)(?::(?:bar)?)?$|,否则|^/foo/(.*?)(?::bar)?:?$|这将是一个替换,所以我们希望在替换:时:bar从最后剥离。我知道这两者并不完全相同,但这并不重要。
|^/foo/(.*?)(?::(?:bar)?)?$|
|^/foo/(.*?)(?::bar)?:?$|
:
:bar
我会使用第一个,因为它只需要检查:一次。第二个可以匹配前三个字符,:bat然后必须回溯,然后:再次检查。此外,第二个可以匹配:bar:,而第一个不能。实际的速度差异很小。第二种方式最好写成/^\/foo\/(.*?)(?::bar|:)?$/
:bat
:bar:
/^\/foo\/(.*?)(?::bar|:)?$/
尽量不要使用正则表达式元字符作为分隔符!