在lib-noir中删除尾部斜杠

时间:2013-09-20 19:21:18

标签: java regex string clojure

我正在研究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中找到任何信息,但找不到正确的信息。

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

1 个答案:

答案 0 :(得分:2)

(?<=.)/$

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

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

正向后视中的表达式是.(正则表达式中的通配符表示任何字符,默认情况下除了换行符),(?<=.)/$仅匹配字符串末尾的正斜杠该字符串在正斜杠之前有另一个字符,换句话说,如果字符串长度至少为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.