我应该使用什么正则表达式将以下 url 匹配为 full_path?
https://edition.cnn.com/search \?q\=test\&size\=10\&category\=us,politics,world,opinion,health
(?:www.|http\:\/\/|https\:\/\/).*}不起作用,它只匹配,www.直到搜索
sm := mux.NewRouter()
sm.HandleFunc("/", getIndex).Methods("GET")
sm.HandleFunc(`/api/v1/hostname/{full_path:(?:www.|http\:\/\/|https\:\/\/).*}`, URLHandler)
更新处理程序是:
func URLHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
url := vars["full_path"]
fmt.Fprintf(w, "Full path is: ", url)
}
编辑2:
这对我有用
sm := mux.NewRouter().SkipClean(true).UseEncodedPath()
sm.HandleFunc("/", getIndex).Methods("GET")
sm.HandleFunc(`/api/v1/hostname/{full_path:.+`, URLHandler)
在我使用的处理程序url.PathUnescape(r.URL.RequestURI())和 xurls上提取 url
func URLHandler(w http.ResponseWriter, r *http.Request) {
URL, _ := url.PathUnescape(r.URL.RequestURI())
ext := xurls.Strict().FindAllString(URL, -1)
fmt.Fprintf(w, "Full path is: ", ext)
}
扬帆大鱼
墨色风雨
随时随地看视频慕课网APP
相关分类