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

查看:309
本文介绍了检查密码是否为“ 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+))(?![.\n])(?=.*[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.

我认为分解正则表达式并一次执行一次会更容易。它还有很多工作要做,但是我敢肯定,维护和调试它会更加容易。这样还可以使您向用户提供更多定向错误消息(不仅仅是无效密码),这将改善用户体验。

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.

从我看到的情况来看,您在regex方面相当流利,因此我认为给定正则表达式来执行所需的操作将是徒劳的。

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 属性应该足够。

包括一个大写字母:您可以使用 [AZ] + 正则表达式。如果字符串包含至少一个大写字母,则此正则表达式将产生 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天全站免登陆