cocos2dx下C++11Regex和android下C語言Regex的使用--------案例密碼校正

來源:互聯網
上載者:User

標籤:cocos2dx   android   Regex   regex   密碼校正   

/************************************************************************//*                           密碼校正                                    *//*                   C++11下沒有問題,但是C語言是有問題的                    *//* 錯誤案例:^[[email protected]#$%^&*()_+`\\-={}\\[\\]:\";'<>?,.\\/]{6,20}$   正確案例:^[][email protected]#$%^&*()_+`={}:;'<>?,./\"\[-]{6,20}$   解答:最噁心的匹配有三個字元 []-- 如果出現在 [] 的開頭或結尾,表示匹配字元 ’-’ ,例如 [^-abc] , [-abc] , [abc-] 。注意不能使用 ’\’ 來轉義] 可以出現在中括弧中的第一個位置,例如 []abc] 或 [^]abc][ 需要轉義*/bool CommonFunc::CheckPasswordLegal(std::string strPassword, int lengthMin, int lengMax){if (lengMax == 0){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  return StringUtil::StringVerification(strPassword, StringUtil::format256("^[[email protected]#$%^&*()_+`\\-={}\\[\\]:\";'<>?,.\\/]+$")); #endif  #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID||CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  return StringUtil::StringVerification(strPassword, StringUtil::format256("^[][email protected]#$%^&*()_+`={}:;'<>?,./\"\[-]+$")); #endif}else if (lengMax != 0){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  return StringUtil::StringVerification(strPassword, StringUtil::format256("^[[email protected]#$%^&*()_+`\\-={}\\[\\]:\";'<>?,.\\/]{%d,%d}$", lengthMin, lengMax)); #endif  #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID||CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  return StringUtil::StringVerification(strPassword, StringUtil::format256("^[][email protected]#$%^&*()_+`={}:;'<>?,./\"\[-]{%d,%d}$", lengthMin, lengMax)); #endif}return false;}bool StringUtil::StringVerification(std::string src, std::string regular){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  regex pattern(regular.c_str());  if ( !regex_match( src, pattern ) )  { return false;}return true;#endif  #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID||CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  regex_t reg;    int retval = regcomp(&reg, regular.c_str(), REG_EXTENDED | REG_NEWLINE);    retval = regexec(&reg, src.c_str(), 0, NULL, 0);    CCLOG("%s is %s\n", regular.c_str(), retval == 0 ? "legal" : "illegal");    if (retval == 0) {regfree(&reg);return true;    }else{return false;}#endif}


難受了我三天,Regex在不同的語言下差別還是有很多,特別是對需要轉義的字元。

終於搞定了,C++11下的Regex是OK,正常轉義。
C語言下的Regex轉義是不行的,必須要按照規範書寫,“-”在最後,“]”在最前,[需要轉義。

參考文檔:

regex——Regex中使用方括弧POSIXRegex - loverszhaokai

cocos2dx下C++11Regex和android下C語言Regex的使用--------案例密碼校正

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.