/************************************************************************//* password Check *//* c++11 no problem, but C is a problematic *//* error case: ^[[email protected]#$% ^&* () _+ ' \\-={}\\[\\]:\; ' <>?,. \\/]{6,20}$ correct case: ^[][email protected]#$%^&* () _+ ' ={}:; ' <>?,./\ "\[-]{6,20}$ solution: The most disgusting match has three characters []--if it appears at the beginning or end of [], indicating a matching character '-', such as [^-ABC], [-ABC], [abc-]. Note that you cannot use ' \ ' to escape the first position that can appear in brackets, such as []ABC] or [^]abc][need to escape */bool Commonfunc::checkpasswordlegal (std::string strpassword, int lengthmin, int lengmax) {if (Lengmax = = 0) {#if (Cc_target_platform = = Cc_platform_win32) return Stringutil::stringveri Fication (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 (®, Regular.c_str (), reg_extended | REG_newline); retval = Regexec (®, Src.c_str (), 0, NULL, 0); Cclog ("%s is%s\n", Regular.c_str (), retval = = 0? "Legal": "illegal"); if (retval = = 0) {regfree (®); return true; }else{return false;} #endif}
It hurts me. For three days, regular expressions differ in different languages, especially for characters that need to be escaped.
Finally, the regular expression under the c++11 is OK, escaped normally.
The regular expression escape in C is not possible, it must be written according to the specification, "-" at the end, "]" in the first, [need to escape.
Reference Documentation:
regex--using square brackets in regular expressions POSIX regular expressions-Loverszhaokai
COCOS2DX c++11 Regular expressions and the use of C language regular expressions under Android--------case password check