【问题标题】:strip trailing slash in lib-noir在 lib-noir 中去除尾部斜杠
【发布时间】:2013-09-20 19:21:18
【问题描述】:

我正在研究 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 #"(?<=.)/$" ""))))

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

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

【问题讨论】:

    标签: java regex string clojure


    【解决方案1】:
    (?<=.)/$
    
    (?<=.)  # Positive lookbehind
    /       # Literal forward slash
    $       # End of line anchor
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多