Go中如何找到最长匹配子串
找到最长子串匹配的正确方法是什么? 我一直在尝试在 Go 中找到与 regexp 包匹配的最长子字符串: https://pkg.go。 dev/regexp
特别是字符串中元音的最长连续匹配。 “aeiou”的任意长度或组合。
这是我现在的代码。它会一直工作,直到字符串变得太长而导致恐慌(超过 1000)。我开始搜索最长的子字符串,并在找到匹配项后返回。
s := "chrononhotonthuooaos"
for i := utf8.RuneCountInString(s); i >=0; i-- {
exp := "[aeiou]{"
exp += fmt.Sprint(i)
exp += "}"
regexStr := regexp.MustCompile(exp)
longestStr := regexStr.FindStringSubmatch(s)
if longestStr != nil {
fmt.Println("longest submatch found:", longestStr)
return utf8.RuneCountInString(longestStr[0])
}
}
return 0
What's the correct way to find the longest substring match?
I’ve been trying to find the longest substring match with the regexp package in Go: https://pkg.go.dev/regexp
Specifically the longest consecutive match of vowels in a string. Any length or combination of "aeiou".
Here's the code I have now. It works until the strings get too long resulting in a panic (over 1000). I start searching for the longest substring and return once a match is found.
s := "chrononhotonthuooaos"
for i := utf8.RuneCountInString(s); i >=0; i-- {
exp := "[aeiou]{"
exp += fmt.Sprint(i)
exp += "}"
regexStr := regexp.MustCompile(exp)
longestStr := regexStr.FindStringSubmatch(s)
if longestStr != nil {
fmt.Println("longest submatch found:", longestStr)
return utf8.RuneCountInString(longestStr[0])
}
}
return 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您感到恐慌的原因是因为您在每次迭代中创建一个新的正则表达式 - 一个 10,000 个字符的字符串?这是 10,000 个已编译的正则表达式,我很确定它们会被缓存。更不用说正则表达式编译的成本很高。
那么为什么要使用正则表达式呢?像这样的东西几乎肯定更快,不会创建任何中间对象,并且以 O(n) 时间和 O(1) 空间复杂度运行。它适用于任何长度的字符串:
https://goplay.tools/snippet/nvZwWeEF8bC
The reason you are getting panics is because your are creating a new regex on every iteration — a 10,000 character string? That's 10,000 compiled regexen, and I'm pretty sure those get cached. Not to mention that regex compilation is expensive.
So why use a regex at all? Something like this is almost certainly faster, doesn't create any intermediate objects, and runs in O(n) time and O(1) space complexity. It will work for strings of any length whatsoever:
https://goplay.tools/snippet/nvZwWeEF8bC
您可以执行以下操作:
https://go.dev/play/p/loxIsR3LMOM
或者,如果没有正则表达式,您可以执行以下操作:
https://go.dev/play/p/XT5-pr3n0tl
You can do the following:
https://go.dev/play/p/loxIsR3LMOM
Or, without regexp, you can do:
https://go.dev/play/p/XT5-pr3n0tl
这是我的答案。虽然它不像同伴那样优雅,但对我来说很有效。 https://go.dev/play/p/ZpZtqt92lvc
}
另一种不使用正则表达式的选项借助小递归函数:https://go.dev/play/p/7sssSWv-0UL
Here is my answer. And although it is not as elegant as that of the companions, it works for me. https://go.dev/play/p/ZpZtqt92lvc
}
Another option without using regex and with the help of little recursive function:https://go.dev/play/p/7sssSWv-0UL