如何编写正则表达式来仅匹配数字、字母和破折号?

发布于 2024-09-09 08:17:15 字数 200 浏览 5 评论 0原文

的表达式

  • 我需要一个仅接受数字
  • 、普通字母(无特殊字符)
  • -

也不允许使用空格。

例子: 正则表达式应匹配:
this-is-quite-alright

它不应该匹配
这个-是/不是,所以正确

I need an expression that will only accept:

  • numbers
  • normal letters (no special characters)
  • -

Spaces are not allowed either.

Example:
The regular expression should match:
this-is-quite-alright

It should not match
this -is/not,soålright

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2024-09-16 08:17:15

您可以使用:

^[A-Za-z0-9-]*$

它匹配完全由大写/小写字母 (ASCII AZ)、数字 (ASCII 0-9) 和破折号组成的字符串(可能为空)。

匹配(如 rubular.com 上所示):

this-is-quite-alright
and-a-1-and-a-2-and-3-4-5

yep---------this-is-also-okay

并拒绝:

this -is/not,soålright
hello world

解释:

  • ^< /code> 和 $ 分别是字符串锚点的开头和结尾
    • 如果您要在字符串中查找匹配项,则不需要锚点

  • [...] 是一个字符类
      字符类中的

    • azAZ0-9 定义范围
    • - 作为类中的最后一个字符是文字​​破折号
  • * 是零或多次重复

regular-expressions.info


变体

规范不清楚,但如果-只是用来分隔“words”,即没有双破折号,没有尾部破折号,没有前面的破折号,那么模式更复杂(只是稍微复杂一点!)

  _"alpha"_    separating dash
 /         \  /
^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$
 \__________/| \__________/|\
    "word"   |    "word"   | zero-or-more
             \_____________/
              group together

这匹配至少一个“单词”的字符串,其中单词由一个或多个“alpha”组成,其中“alpha”由字母和数字组成。后面可以有更多“单词”,并且它们总是用破折号分隔。

这匹配(如 rubular.com 上所示):

this-is-quite-alright
and-a-1-and-a-2-and-3-4-5

并拒绝:

--no-way
no-way--
no--way

You can use:

^[A-Za-z0-9-]*$

This matches strings, possibly empty, that is wholly composed of uppercase/lowercase letters (ASCII A-Z), digits (ASCII 0-9), and a dash.

This matches (as seen on rubular.com):

this-is-quite-alright
and-a-1-and-a-2-and-3-4-5

yep---------this-is-also-okay

And rejects:

this -is/not,soålright
hello world

Explanation:

  • ^ and $ are beginning and end of string anchors respectively
    • If you're looking for matches within a string, then you don't need the anchors
  • [...] is a character class
    • a-z, A-Z, 0-9 in a character class define ranges
    • - as a last character in a class is a literal dash
  • * is zero-or-more repetition

regular-expressions.info


Variation

The specification was not clear, but if - is only to be used to separate "words", i.e. no double dash, no trailing dash, no preceding dash, then the pattern is more complex (only slightly!)

  _"alpha"_    separating dash
 /         \  /
^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$
 \__________/| \__________/|\
    "word"   |    "word"   | zero-or-more
             \_____________/
              group together

This matches strings that is at least one "word", where words consists of one or more "alpha", where "alpha" consists of letters and numbers. More "words" can follow, and they're always separated by a dash.

This matches (as seen on rubular.com):

this-is-quite-alright
and-a-1-and-a-2-and-3-4-5

And rejects:

--no-way
no-way--
no--way
囚我心虐我身 2024-09-16 08:17:15
[A-z0-9-]+

但你的问题很令人困惑,因为它要求输入字母和数字,并且有一个包含破折号的示例。

[A-z0-9-]+

But your question is confusing as it asks for letters and numbers and has an example containing a dash.

前事休说 2024-09-16 08:17:15

这是一个社区 wiki,尝试编译有关此“URL/SEO slugging”主题的相关问题的链接。邀请社区做出贡献。

相关问题

相关标签

This is a community wiki, an attempt to compile links to related questions about this "URL/SEO slugging" topic. Community is invited to contribute.

Related questions

Related tags

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文