go语言 正则表达式

老青蛙嘎嘎嘎 2014-03-03 10:01:24
下面这段代码摘自 martini 库
func newRoute(method string, pattern string, handlers []Handler) route {
route := route{method, nil, handlers}
r := regexp.MustCompile(`:[^/#?()\.\\]+`)
pattern = r.ReplaceAllStringFunc(pattern, func(m string) string {
return fmt.Sprintf(`(?P<%s>[^/#?]+)`, m[1:len(m)])

})
pattern += `\/?`
route.regex = regexp.MustCompile(pattern)
return route
}

谁能解释下该函数中的正则表达式啊?
...全文
122 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
老青蛙嘎嘎嘎 2014-03-06
  • 打赏
  • 举报
回复
r := regexp.MustCompile(`:[^/#?()\.\\]+`)
感觉可以写成
r := regexp.MustCompile(`:[^/#?().\\]+`)
pattern += `\/?`
可以写成
pattern += `/?`
不知道正确不正确
十一文 2014-03-03
  • 打赏
  • 举报
回复
`:[^/#?()\.\\]+` 匹配所有非 / # ? ( ) 。\ 字符
老青蛙嘎嘎嘎 2014-03-03
  • 打赏
  • 举报
回复
现在需要找本书学习下正则表达式
李察德-泰森 2014-03-03
  • 打赏
  • 举报
回复
引用 2 楼 gutsyfarmer 的回复:
pattern += `\/?` 这一句什么意思?目的是什么啊?
pattern = r.ReplaceAllStringFunc(pattern, func(m string) string { return fmt.Sprintf(`(?P<%s>[^/#?]+)`, m[1:len(m)]) }) 上面这句你看懂了就知道下面这句是干啥的了, 你去这里看说明:http://golang.org/pkg/regexp/#Regexp.ReplaceAllStringFunc
老青蛙嘎嘎嘎 2014-03-03
  • 打赏
  • 举报
回复
pattern += `\/?` 这一句什么意思?目的是什么啊?

2,190

社区成员

发帖
与我相关
我的任务
社区描述
go语言学习与交流版
社区管理员
  • go语言社区
  • Freeman Z
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧