2014-09-01 52 views
-3

我想在c編程中使用以下規則驗證密碼。在c編程驗證密碼

  • 至少一個大寫的字

  • 至少一個小寫字

  • 至少一個數字

  • 至少一個符號(!@#$%^ & *)

  • 長度:8 - 32

我該如何使用正則表達式或不使用它?

+2

那麼你嘗試過什麼?有一些代碼讓我們看看? – Joe 2014-09-01 08:29:09

+0

你可能有一個循環,並使用像[isalpha(3)]這樣的宏(http://man7.org/linux/man-pages/man3/isalpha.3.html);是輸入的UTF-8? – 2014-09-01 08:33:30

+0

我試過了(regcomp(&re,「^(?=(。* \ d))(?=。* [az])(?=。* [AZ])(?=。* [!@#$%^ &*])。{8,32} $「,REG_EXTENDED)!= 0)但它會生成錯誤代碼1. – user3693951 2014-09-01 08:34:46

回答

0

你可以試試下面的正則表達式,以滿足您的所有要求,

^(?=.{8,32}$)(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[([email protected]#$%^&*)]).* 

DEMO

^(?=.{8,32}$)   -  length: 8 - 32 
(?=.*?[A-Z])   -  at-least one uppercase letter. 
(?=.*?[a-z])   -  at-least one lowercase letter. 
(?=.*?[0-9])   -  at-least one number. 
(?=.*?[([email protected]#$%^&*)]) -  at-least one symbol present inside the character class. 
.*     -  Match any character zero or more times only if all the above 5 conditions are true. 
+0

ret = regcomp(&re,「^(?=。{8,32} $)(?=。*?[AZ])(?=。*?[az])(?=。*?[0-9 ])(?=。*?[(!@#$%^&*)])。*「,REG_EXTENDED),它會生成錯誤代碼1 – user3693951 2014-09-01 08:52:27

+0

問題出在您的代碼中,而不是在正則表達式中。請參閱演示網站.. – 2014-09-01 08:53:39

+0

請參閱此處http://regex101.com/r/iX8hF3/8。你的正則表達式不能匹配第二行 – 2014-09-01 08:57:48