英文:
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
匹配后期望得到以下字符串
- 1221a7f47-84c1-445e-a615-ff82d92e2eaa
- article
- jane
- 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
- 1221a7f47-84c1-445e-a615-ff82d92e2eaa
- article
- jane
- 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*)")
Using raw string literals (`)
re := regexp.MustCompile(`(([a-f0-9]+\-)+[a-f0-9]+)\/(.*?)\/(.*?);version=(\d*)`)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论