英文:
Removing excessive dashes with OR regex
问题
我正在尝试从字符串中删除所有多余的破折号。
StateFileRegexDashes = regexp.MustCompile(
--+?|^-+?|-+$)
上面的正则表达式在 regex101 上对我有效,但在我的代码中却不起作用。
我认为可能是因为 'g|global modifier' 的问题,但我认为在 ReplaceAllString()
中的 'All' 部分已经解决了这个问题。
Golang Playground: https://play.golang.org/p/ZR72gZEhZ_
Regex101: https://regex101.com/r/tUYHo1/1
编辑:
问题是我用破折号替换了所有内容,而实际上我只想删除前缀和尾随的破折号。
我不再使用另一个正则表达式,而是现在使用 strings.Trim()
。
var (
StateFileRegex = regexp.MustCompile(`[\/\?<>\\:\*\|":!\s.]`)
StateFileRegexDashes = regexp.MustCompile(`--+`)
)
func hashBotStateFile(name string) string {
lower := strings.ToLower(name)
dashes := StateFileRegex.ReplaceAllString(lower, StateFileCharReplacer)
singles := StateFileRegexDashes.ReplaceAllString(dashes, StateFileCharReplacer)
trimmed := strings.Trim(singles, "-")
return filepath.Join(StateFileFolder, fmt.Sprintf("%s.json", trimmed))
}
英文:
I'm trying to remove all excessive dashes from strings.
StateFileRegexDashes = regexp.MustCompile(
--+?|^-+?|-+$)
The above regex seems to work for me on regex101, but not within my code.
I was thinking it could be because of the 'g|global modifier', but I assume that is solved by the 'All' part in ReplaceAllString()
Golang Playground: https://play.golang.org/p/ZR72gZEhZ_
Regex101: https://regex101.com/r/tUYHo1/1
Edit:
Problem was I was replacing everything with - dashes, where I actually wanted to remove prefixed and trailing dashes.
Instead of doing another regex, I'm just using strings.Trim()
now.
var (
StateFileRegex = regexp.MustCompile(`[\/\?<>\\:\*\|":!\s.]`)
StateFileRegexDashes = regexp.MustCompile(`--+`)
)
func hashBotStateFile(name string) string {
lower := strings.ToLower(name)
dashes := StateFileRegex.ReplaceAllString(lower, StateFileCharReplacer)
singles := StateFileRegexDashes.ReplaceAllString(dashes, StateFileCharReplacer)
trimmed := strings.Trim(singles, "-")
return filepath.Join(StateFileFolder, fmt.Sprintf("%s.json", trimmed))
}
答案1
得分: 3
你需要移除字符串中的前导和尾随的-
符号,并缩小字符串中间的连字符。你需要将连字符的正则表达式拆分为以下两个:
StateFileRegexDashes = regexp.MustCompile(`--+`)
StateFileRegexDashesRemove = regexp.MustCompile(`^-+|-+$`)
然后可以使用以下代码:
func hashBotStateFile(name string) string {
lower := strings.ToLower(name)
dashes := StateFileRegex.ReplaceAllString(lower, StateFileCharReplacer)
trimmed := StateFileRegexDashes.ReplaceAllString(dashes, StateFileCharReplacer)
final := StateFileRegexDashesRemove.ReplaceAllString(trimmed, "")
return filepath.Join(StateFileFolder, fmt.Sprintf("%s.json", final))
}
请参考Go演示。
另外,请注意,模式末尾的--+?
将始终只匹配2个连字符,你需要将懒惰量词改为贪婪量词。
英文:
You need to remove the leading/trailing -
symbols, and shrink those inside the string. You need to split the dashes regex into
StateFileRegexDashes = regexp.MustCompile(`--+`)
StateFileRegexDashesRemove = regexp.MustCompile(`^-+|-+$`)
And then use something like
func hashBotStateFile(name string) string {
lower := strings.ToLower(name)
dashes := StateFileRegex.ReplaceAllString(lower, StateFileCharReplacer)
trimmed := StateFileRegexDashes.ReplaceAllString(dashes, StateFileCharReplacer)
final := StateFileRegexDashesRemove.ReplaceAllString(trimmed, "")
return filepath.Join(StateFileFolder, fmt.Sprintf("%s.json", final))
}
See the Go demo
Also, note that --+?
at the end of the pattern will always match only 2 hyphens, you need to turn the lazy quantifiers into greedy ones.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论