gpt4 book ai didi

仅包含字母数字字符和下划线的正则表达式,以字母字符开头

转载 作者:行者123 更新时间:2023-12-04 14:56:41 24 4
gpt4 key购买 nike

我正在尝试验证 Firebase 事件名称:

Event names can be up to 40 characters long, may only contain alphanumeric characters and underscores ("_"), and must start with an alphabetic character.

无效:

  • 4foo(以数字开头)
  • foo bar(包含一个空格)
  • foo$@bar(包含特殊字符)

有效:

  • foo
  • foo_bar
  • Foo_Bar

我试过 \d?[^A-Za-z0-9_]+ 如果有任何特殊字符和空格,它会匹配,但它不会匹配带有数字字符的字符串开始。

最佳答案

使用

^[A-Za-z][A-Za-z0-9_]{0,39}$

参见 regex proof .

同义词:

^[A-Za-z]\w{0,39}$
^\p{L}\w{0,39}$
^[[:alpha:]]\w{0,39}$

解释

--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[A-Za-z] any character of: 'A' to 'Z', 'a' to 'z'
--------------------------------------------------------------------------------
[A-Za-z0-9_]{0,39} any character of: 'A' to 'Z', 'a' to 'z',
'0' to '9', '_' (between 0 and 39 times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

关于仅包含字母数字字符和下划线的正则表达式,以字母字符开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67834604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com