dousao8152 2015-07-27 16:39
浏览 2609
已采纳

Golang regexp问题与FindStringSubmatch

I was trying to use regexp to do some pattern matching with the or operator but I got some odd results. I have stripped out everything but the essentials to show the problem with my result.

This is my code:

package main

  1. import "fmt"
  2. import "regexp"
  3. func main() {
  4. authRegexp := regexp.MustCompile("^token=(llll|(.+))$")
  5. matches := authRegexp.FindStringSubmatch("token=llll")
  6. fmt.Println("MATCHES", matches, len(matches))
  7. // MATCHES [token=llll llll ] 3
  8. }

Url: http://play.golang.org/p/nLtWQQgveY

The matches array has a length of 3, when I think it should have a length of 2. The last value is an empty string. I dont understand why it does this. Is this a golang bug? How do I submit golang bugs?

  • 写回答

2条回答 默认 最新

  • douxingti9307 2015-07-27 16:50
    关注

    The last empty value corresponds to (.+) and just an indication that this capturing group was not 'hit' while matching. In other words, it is completely legitimate. In your case it will be safe to use non-capturing group instead: (?:.+) - http://play.golang.org/p/MEkkoGqxho

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部