1. 程式人生 > >MFC利用正則表示式進行密碼匹配

MFC利用正則表示式進行密碼匹配

最近用到MFC中對密碼進行驗證,因此對正則表示式進行了簡單的研究,總結如下:

1. 標頭檔案支援: 

#include <regex>

2. std::regex不支援CString型別的模式設定和字串匹配,需要進行轉換, 可以考慮將其轉換為std::string.

CString cstrPassword;
CT2CA tempPassword(cstrPassword);
std::string strPassword(tempPassword);

3. 正則表示式的應用
std::regex regPattern("[0-9a-zA-Z]{6,}");<span style="white-space:pre">	</span>// at least 6 charaters of digit or letter
BOOL bValid = std::regex_match(strPassword, regPattern);

其他較複雜密碼規則的正則表示式如:
^(?=.*\d+)(?=.*[a-z]+)(?=.*[A-Z]+).*$<span style="white-space:pre">	</span>// contains at least one digit, one lowercase letter and one uppercase letter

以上。