C#正則表達式
回答
^(?=.*[A-Z])(?=.*[0-9])[A-Za-z0-9]+$
應該做的。
^ # start of string
(?=.*[A-Z]) # assert that there is at least one capital letter ahead
(?=.*[0-9]) # assert that there is at least one digit ahead
[A-Za-z0-9]+ # match any number of allowed characters
# Use {8,} instead of + to require a minimum length of 8 characters.
$ # end of string
您可以在正則表達式中使用non-zero-width lookahead/lookbehind assertions。例如:
^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$
要求存在至少一個數字,一個小寫字母和一個大寫字母。使用\w
可以讓您接受非英文或重音字符(您可能希望或不希望允許)。否則,請使用[a-zA-Z]。
這比OP想要的要多得多('\ w'是.NET中的Unicode-aware) - 儘管當然限制有效字母爲*密碼*也沒有多大意義。 – 2010-10-29 20:37:56
@Tim Pietzcker:是的,我知道。我提到'\ w'將接受來自unicode集的重音和國際字符。這是使用[a-zA-Z]構造的替代方案。 – LBushkin 2010-10-29 20:39:42
對不起,我沒有仔細閱讀你的答案。但是'\ w'也匹配數字和下劃線(以及其他的「連續標點符號」字符)。 – 2010-10-29 20:44:49
bool valid =
Regex.IsMatch(password, @"\w+")// add additional allowable characters here
&& Regex.IsMatch(password, @"\d")
&& Regex.IsMatch(password, @"\p{Lu}");
[求助與密碼複雜性的正則表達式(的
- 1. 正則表達式(正則表達式)
- 2. 正則表達式(正則表達式)
- 3. 正則表達式(正則表達式)
- 4. C++正則表達式與提升正則表達式
- 5. 正則表達式的C#正則表達式
- 6. C#正則表達式替換爲正則表達式
- 7. 轉換PHP正則表達式C#正則表達式
- 8. 轉換C#正則表達式的JavaScript正則表達式
- 9. 正則表達式在c中的正則表達式#
- 10. Javascript正則表達式到c#正則表達式
- 11. C#正則表達式分割和正則表達式
- 12. 將C#正則表達式轉換爲JavaScript正則表達式
- 13. 正則表達式使用GNU C正則表達式庫
- 14. 轉換C#正則表達式的JavaScript正則表達式
- 15. Objective-C和正則表達式中的正則表達式
- 16. C#正則表達式NSRegularExpression
- 17. 正則表達式在C#
- 18. 正則表達式C#
- 19. 正則表達式Objective-c
- 20. C#和正則表達式
- 21. 正則表達式C#
- 22. 子正則表達式C#
- 23. 正則表達式C#IsMatch()
- 24. 正則表達式C#
- 25. Objective C正則表達式?
- 26. C#正則表達式組
- 27. C#的正則表達式
- 28. C#正則表達式
- 29. 用C#正則表達式
- 30. C++正則表達式
可能重複http://stackoverflow.com/questions/2582079/help-with-password-complexity -regex) – 2010-10-29 20:36:11