正则表达式帮助匹配
您好,我需要提出一个有效的正则表达式,它可以是任何以字母或下划线开头的标识符名称,但可以包含任意数量的字母、下划线和/或数字(所有字母都可以是大写或小写)。 例如,您的正则表达式应匹配以下文本字符串:“_”、“x2”和“This_is_valid”。它不应匹配这些文本字符串:“2days”或“invalid_variable%”。
到目前为止,这是我想出的,但我认为这是不对的
/^[_\w][^\W]+/
Hello I need coming up with a valid regular expression It could be any identifier name that starts with a letter or underscore but may contain any number of letters, underscores, and/or digits (all letters may be upper or lower case).
For example, your regular expression should match the following text strings: “_”, “x2”, and “This_is_valid” It should not match these text strings: “2days”, or “invalid_variable%”.
So far this is what I have came up with but I don't think it is right
/^[_\w][^\W]+/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下内容有效:
以 (
^
) 字母(大写或小写)或下划线 ([_a-zA-Z]
) 开头,后跟任意数量的字母,数字或下划线 (\w
) 到末尾 ($
)了解有关 Perl 中的正则表达式
The following will work:
Starts with (
^
) a letter (upper or lowercase) or underscore ([_a-zA-Z]
), followed by any amount of letter, digit, or underscore (\w
) to the end ($
)Read more about Regular Expressions in Perl
也许下面的正则表达式:
Maybe the below regex:
如果标识位于字符串的开头,那么很容易
/^(_|[a-zA-Z]).*/
如果它嵌入在较长的字符串中,我想这并不多更糟糕的是,假设它是一个单词的开头...
/\s(_|[a-zA-Z]).*/
If the identify is at the start of a string, then it's easy
/^(_|[a-zA-Z]).*/
If it's embedded in a longer string, I guess it's not much worse, assuming it's the start of a word...
/\s(_|[a-zA-Z]).*/