正则表达式检查密码是否为“8 个字符,包括 1 个大写字母、1 个特殊字符、字母数字字符"; [英] Regular expression to check if password is "8 characters including 1 uppercase letter, 1 special character, alphanumeric characters"

查看:44
本文介绍了正则表达式检查密码是否为“8 个字符,包括 1 个大写字母、1 个特殊字符、字母数字字符";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个正则表达式来检查

I want a regular expression to check that

密码必须为八个字符,包括一个大写字母、一个特殊字符和字母数字字符.

a password must be eight characters including one uppercase letter, one special character and alphanumeric characters.

这是我的验证表达式,它是八个字符,包括一个大写字母、一个小写字母和一个数字或特殊字符.

And here is my validation expression which is for eight characters including one uppercase letter, one lowercase letter, and one number or special character.

(?=^.{8,}$)((?=.*d)|(?=.*W+))(?![.
])(?=.*[A-Z])(?=.*[a-z]).*$"

密码必须为八个字符,包括一个大写字母、一个特殊字符和字母数字字符,我该如何写?

How I can write it for a password that must be eight characters including one uppercase letter, one special character and alphanumeric characters?

推荐答案

您所追求的正则表达式很可能是巨大的,并且是维护的噩梦,尤其是对于不熟悉正则表达式的人.

The regular expression you are after will most likely be huge and a nightmare to maintain especially for people who are not that familiar with regular expressions.

我认为分解您的正则表达式并一次做一点会更容易.可能需要做更多的工作,但我很确定维护和调试它会更容易.这还允许您向用户提供更多定向错误消息(而不仅仅是 Invalid Password),从而改善用户体验.

I think it would be easier to break your regex down and do it one bit at a time. It might take a bit more to do, but I am pretty sure that maintaining it and debugging it would be easier. This would also allow you to provide more directed error messages to your users (other than just Invalid Password) which should improve user experience.

据我所知,你对正则表达式非常流利,所以我认为给你提供正则表达式来做你需要的事情是徒劳的.

From what I am seeing you are pretty fluent in regex, so I would presume that giving you the regular expressions to do what you need would be futile.

看到你的评论,我会这样做:

Seeing your comment, this is how I would go about it:

  • 必须是八个字符长:您不需要为此使用正则表达式.使用 .Length 属性就足够了.

包括一个大写字母:您可以使用 [A-Z]+ 正则表达式.如果字符串包含至少一个大写字母,则此正则表达式将产生 true.

Including one uppercase letter: You can use the [A-Z]+ regular expression. If the string contains at least one upper case letter, this regular expression will yield true.

一个特殊字符:您可以使用 W 来匹配任何不是字母或数字的字符,否则,您可以使用类似 [!@#] 指定特殊字符的自定义列表.请注意,尽管 $^() 等字符是正则表达式语言中的特殊字符,所以他们需要像这样被转义:$.简而言之,您可以使用 W.

One special character: You can use either the W which will match any character which is not a letter or a number or else, you can use something like so [!@#] to specify a custom list of special characters. Note though that characters such as $, ^, ( and ) are special characters in the regular expression language, so they need to be escaped like so: $. So in short, you might use the W.

字母数字字符:使用 w+ 应该匹配任何字母、数字和下划线.

Alphanumeric characters: Using the w+ should match any letter and number and underscore.

查看教程了解更多信息.

Take a look at this tutorial for more information.

这篇关于正则表达式检查密码是否为“8 个字符,包括 1 个大写字母、1 个特殊字符、字母数字字符";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆