英文:
Golang to match dash(hyphen) character
问题
Golang匹配破折号字符
regexp.MustCompile(`[^[:alnum:]\s]`)
这个正则表达式可以匹配-
(破折号)字符。
但是我想要一个排除破折号字符的正则表达式。
我尝试了以下两个,但它们都匹配了+
字符:
regexp.MustCompile(`[^[0-9A-Za-z\-]\s]`)
regexp.MustCompile(`[^[0-9A-Za-z-]\s]`)
如何匹配既不是字母数字字符
又不是-
(破折号)字符的字符?
英文:
Golang to match dash(hyphen) character
regexp.MustCompile(`[^[:alnum:]\s]`)
This matches -
(dash) character
But I want a regex that excludes the dash character.
I tried the following but it greps the +
characters:
regexp.MustCompile(`[^[0-9A-Za-z\-]\s]`)
regexp.MustCompile(`[^[0-9A-Za-z-]\s]`)
How do I match characters that are not alphanumeric
and not -
(dash)?
答案1
得分: 2
如何匹配非字母数字、非破折号(-)和非空格的字符?
将正则表达式中多余的字符类删除。上述的正则表达式是正确的。
英文:
> How do I match characters that are not alphanumeric ,not -(dash) and not a space?
[^A-Za-z0-9\s-]
Remove the extra character classes from your regex. The above regex would be fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论