大猩猩Mux正则表达式 [英] Gorilla Mux Regex

查看:61
本文介绍了大猩猩Mux正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Golang Gorilla Toolkit 中的Mux包用于我的路线.

I'm using the Mux package from the Golang Gorilla Toolkit for my routes.

请考虑以下路线:

m.HandleFunc("/admin/install", installHandler).Methods("GET")
m.HandleFunc("/admin/^((?!install).)*$", adminHandler).Methods("GET")
m.HandleFunc("/admin", adminHandler).Methods("GET")

问题出在中间路线的正则表达式上-无法解释,因此该路线将无法工作!

The problem is with the regex of the middle route - it is not interpreted, so the route will not work!

m.HandleFunc("/admin/{^((?!install).)*$}", adminHandler).Methods("GET")

使用{}大括号也不起作用.它只是被忽略,并被视为/admin/

With the {} curly brackets doesn't work either. It is just ignored, and treated as /admin/

都不是:

m.HandleFunc("/admin/{_dummy:^((?!install).)*$}", adminHandler).Methods("GET")

简而言之,我要在这里实现的目标是首先匹配/admin/install路由,然后使用正则表达式将该确切的路由从下面的路由中排除,但这是行不通的.

In short, what I'm trying to achieve here is to first match the /admin/install route, and that exact route I then want to exclude from the route below, using the regex, but it doesn't work.

是否可以将regex与大猩猩mux包一起使用?

Is there some way to use regex with the gorilla mux package?

推荐答案

实际上可以做到这一点:

It is actually possible to do this:

m.HandleFunc(`/{_dummy:admin\/([^install]*).*}`, adminHandler).Methods("GET")

作为我对VonC上面评论的回答,这里有一个样例应用程序: https://play.golang.org/p/nYWNADK7Sr

As my answer to VonC's comment above, here a sample go app: https://play.golang.org/p/nYWNADK7Sr

在本地PC上运行它.尝试以下路线:

Run it on your local pc. Try the following routes:

http://localhost:8080/admin/ - (returns "adminHandler")
http://localhost:8080/admin/something - (returns "adminHandler")
http://localhost:8080/admin/install - (returns "installHandler")

是的,VonC确实解决了特定问题:

So yes VonC, it does solve the specific problem:

"以首先匹配/admin/install路由,然后再匹配该确切路由想要从下面的路线中排除"

"to first match the /admin/install route, and that exact route I then want to exclude from the route below"

但这是正确的,它并不意味着不跟单词,安装" -这只是在re2语法范围内可能的另一种方法.如果碰巧出现在网址中,则只是忽略"或排除安装一词.

But it is correct that it doesn't mean "not followed by the word, install" - this is just an alternative approach which is possible within the bounds of the re2 syntax. To simply "ignore" or exclude the word install, if it happens to show up in the url.

这篇关于大猩猩Mux正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆