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

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

最佳答案

(?<=.)/$

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


后面的正向是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.

关于java - lib-noir中的条尾斜杠,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18924323/

10-10 01:03
查看更多