What does a-z-A-Z mean in a regular expression?

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

What does a-z-A-Z mean in a regular expression?

问题

我在使用别人的代码,遇到了正则表达式[^0-9a-z-A-Z]。这与常见的[^0-9a-zA-Z]非常相似,意思是排除非字母数字字符,但请注意中间多了一个破折号,位于小写字母z和大写字母A之间。

我对正则表达式不太熟悉,但我已经阅读了几篇相关的页面,没有看到任何规则涵盖这种语法的含义。也许这甚至不是有效的语法,但是Golang的正则表达式解释器似乎并不在意。我希望能得到任何解释。谢谢。

英文:

I've been working with someone else's code and I ran across the regular expression [^0-9a-z-A-Z]. This bears close resemblance to the common [^0-9a-zA-Z] which is meant to exclude non-alphanumeric characters, but note the extra dash in the middle, between the lowercase z and uppercase A.

I'm not very familiar with regular expressions, but I've read several pages on them now, and none of the rules I've seen seem to cover what this syntax would mean. Perhaps it's not even valid syntax, but the Golang regex interpreter doesn't seem to mind. I'd appreciate any clarification. Thanks.

答案1

得分: 52

在无法解释为范围的位置上,字符类中的破折号被解释为字面破折号。因此,该表达式排除了字符09azAZ-。这就是为什么没有语法错误。

不过,这可能是一个打字错误。如果破折号是有意放在那里的,为了避免混淆,应该对其进行转义和/或将其移出范围之间,例如[^0-9a-zA-Z\-]

英文:

A dash in a character class in a place where it cannot be interpreted as a range is interpreted as a literal dash. So the expression excludes the characters 0 to 9, a to z, A to Z, and -. That's why there's no syntax error.

It's probably a typo though. If the dash is meant to be there, then to prevent confusion it should be escaped and/or moved out from between the ranges, such as [^0-9a-zA-Z\-]

答案2

得分: 0

这是要翻译的内容:

它排除了减号。

你可以在这里方便地测试正则表达式:http://www.regexr.com/

英文:

It excludes the minus sign.

You can test regex handily here: http://www.regexr.com/

huangapple
  • 本文由 发表于 2015年5月10日 04:12:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/30144524.html
匿名

发表评论

匿名网友

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

确定