doujia9204 2015-03-09 19:50
浏览 96
已采纳

大猩猩Mux正则表达式

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

Consider the following 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")

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

Neither does:

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

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.

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

  • 写回答

2条回答 默认 最新

  • dongyong5255 2015-03-23 22:24
    关注

    It is actually possible to do this:

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

    Edit:

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

    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")
    

    So yes VonC, it does solve the specific problem:

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格