0

我正在研究 lib-noir 图书馆。当我研究wrap-strip-trailing-slash函数时,我发现了有趣的正则表达式模式。

(defn wrap-strip-trailing-slash
  "If the requested url has a trailing slash, remove it."
  [handler]
  (fn [request]
    (handler (update-in request [:uri] s/replace #"(?<=.)/$" ""))))

作者使用#"(?<=.)/$"了模式,但我不明白正则表达式在这种情况下是如何工作的?我试图从 Java Regex Document 中查找任何信息,但找不到正确的信息。

(?<=.)它看起来很有趣。请帮助我理解这一点。

4

1 回答 1

2
(?<=.)/$

(?<=.)  # Positive lookbehind
/       # Literal forward slash
$       # End of line anchor

肯定的lookbehind是一个lookaround断言,它确保后面的字符在它之前有一些与断言中的表达式匹配的东西。

正则表达式中的表达式是.(正则表达式中的通配符表示任何字符,默认情况下除了换行符之外),(?<=.)/$仅当该字符串在正斜杠之前有另一个字符时,才会匹配字符串末尾的正斜杠,换句话说,如果字符串至少有 2 个字符长。

/    # No replace
a/   # Replace the / so that you have the string "a" as result.
a/a  # No replace because / is not at the end of the string.
于 2013-09-20T20:36:12.350 回答