regular expressions for the guid

huangapple go评论93阅读模式
英文:

regular expressions for the guid

问题

我遇到了一些关于正则表达式的问题。

> 错误:无效操作

re := regexp.MustCompile("(([a-f0-9]+\-)+[a-f0-9]+)\/(.*?)\/(.*?);version=(\d*)")
match := re.FindStringSubmatch(hex.EncodeToString([]byte(href)))
fmt.Println(match)

我尝试匹配的测试字符串是

/data/1221a7f47-84c1-445e-a615-ff82d92e2eaa/article/jane;version=1493756861347

/data/1221a7f47-84c1-445e-a615-ff82d92e2eaa/article/john;version=1493756856398

匹配后期望得到以下字符串

  1. 1221a7f47-84c1-445e-a615-ff82d92e2eaa
  2. article
  3. jane
  4. 1493756856398
英文:

I'm having some issues with following regular expressions.

> Error: Invalid operation

re := regexp.MustCompile("(([a-f0-9]+\-)+[a-f0-9]+)\/(.*?)\/(.*?);version=(\d*)")
match := re.FindStringSubmatch(hex.EncodeToString([]byte(href)))
fmt.Println(match)

my test strings that I'm trying to match are

/data/1221a7f47-84c1-445e-a615-ff82d92e2eaa/article/jane;version=1493756861347

/data/1221a7f47-84c1-445e-a615-ff82d92e2eaa/article/john;version=1493756856398

Expecting following strings after match

  1. 1221a7f47-84c1-445e-a615-ff82d92e2eaa
  2. article
  3. jane
  4. 1493756856398

答案1

得分: 1

你需要修复第一行,正确声明正则表达式。尝试以下方法:

使用反斜杠(进行转义)

re := regexp.MustCompile("(([a-f0-9]+\\-)+[a-f0-9]+)\\/(.*?)\\/(.*?);version=(\\d*)")

试一试

使用原始字符串字面量(`)

re := regexp.MustCompile(`(([a-f0-9]+\-)+[a-f0-9]+)\/(.*?)\/(.*?);version=(\d*)`)

试一试

英文:

You need to fix the first line, declare the regex correctly. Try these:

Using backslashes (to escape)

re := regexp.MustCompile("(([a-f0-9]+\\-)+[a-f0-9]+)\\/(.*?)\\/(.*?);version=(\\d*)")

Try it out

Using raw string literals (`)

re := regexp.MustCompile(`(([a-f0-9]+\-)+[a-f0-9]+)\/(.*?)\/(.*?);version=(\d*)`)

Try it out

huangapple
  • 本文由 发表于 2017年5月3日 06:02:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/43747954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定