Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

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

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?

(如何为必须包含八个大写字母(一个大写字母,一个特殊字符和字母数字字符)的密码编写密码?)

  ask by Rania Umair translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(这还将使您能够向用户提供更多定向错误消息(不仅仅是Invalid Password ),这将改善用户体验。)

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.

(从我所看到的来看,您在regex方面相当流利,所以我认为为您提供正则表达式来做您需要的事情是徒劳的。)

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

(看到您的评论,这就是我的处理方式:)

  • Must be eight characters Long: You do not need a regex for this.

    (长度必须为八个字符:为此,您不需要正则表达式。)

    Using the .Length property should be enough.

    (使用.Length属性应该足够了。)

  • Including one uppercase letter: You can use the [AZ]+ regular expression.

    (包括一个大写字母:您可以使用[AZ]+正则表达式。)

    If the string contains at least one upper case letter, this regular expression will yield true .

    (如果字符串包含至少一个大写字母,则此正则表达式将产生true 。)

  • 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.

    (一个特殊字符:可以使用\W来匹配非字母或数字的任何字符,否则可以使用[!@#]这样的东西来指定特殊字符的自定义列表。)

    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.

    (字母数字字符:使用\w+应该匹配任何字母,数字和下划线。)

Take a look at this tutorial for more information.

(查看教程以获取更多信息。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...