将Golang与破折号字符匹配

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

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.

huangapple
  • 本文由 发表于 2014年12月30日 14:18:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/27700524.html
匿名

发表评论

匿名网友

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

确定