字母数字正则表达式模式匹配

发布于 2024-12-17 06:58:38 字数 132 浏览 4 评论 0原文

我需要一个匹配字母数字模式(完全数字模式除外)的正则表达式。

asdfgesod valid
1asdndwdd valid
asd124asd valid
a2asd43bd valid
123346678 invalid

I need a regex that matches the alphanumeric patterns except totally numeric ones.

asdfgesod valid
1asdndwdd valid
asd124asd valid
a2asd43bd valid
123346678 invalid

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

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

发布评论

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

评论(4

望喜 2024-12-24 06:58:38
/^[a-z0-9]*[a-z][a-z0-9]*$/i

分解:

^[a-z0-9]* - 字符串以任意数量(包括零)的字母数字字符开头

[az] - 字符串有一个 az 字符

[a-z0-9]*$ - 字符串以任意数量(包括零)的字母数字字符结尾

/^[a-z0-9]*[a-z][a-z0-9]*$/i

Broken down:

^[a-z0-9]* - String starts with any number (including zero) of alphanumeric characters

[a-z] - String has an a-z character

[a-z0-9]*$ - String ends with any number (including zero) of alphanumeric characters

↘紸啶 2024-12-24 06:58:38

您可以尝试:

/^(?![0-9]+$)[a-z0-9]+$/i

查看

[a-z0-9]+$ 确保该字符串仅由字母数字字符组成。

负向先行 (?![0-9]+$) 确保字符串不全是数字。

You can try:

/^(?![0-9]+$)[a-z0-9]+$/i

See it

[a-z0-9]+$ ensures the string is made of only alphanumeric characters.

The negative lookahead (?![0-9]+$) ensures the string is not all numeric.

债姬 2024-12-24 06:58:38

解决方案是首先检查字符串是否仅包含数字,如果不包含,则检查字符串的其余部分。以下正则表达式可以做到这一点:

^(?![\d]+$)[a-z0-9]+$

在此处查看其实际情况:http://regexr.com?2v8n5

A solution would be to first check if the string contains only digits, if not check rest of the string. The following regex would do that:

^(?![\d]+$)[a-z0-9]+$

See it in action here: http://regexr.com?2v8n5

仙气飘飘 2024-12-24 06:58:38

在 java StringUtils.isNumeric(String value) 的情况下使用检查字符串是否仅包含数字。

顺便说一句:如果字符串与正则表达式匹配: "^\d*$" 这意味着它只包含数字。

Use in case of java StringUtils.isNumeric(String value) check if string contains only numbers.

BTW: if string matches regex: "^\d*$" this means that it contains only digits.

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