Use the C ++ 11 Regular Expression in cocos2dx and the C language Regular Expression in android -------- case password verification, cocos2dxandroid
/*************************************** * ******************************** // * Password verification */ /* C ++ 11, but the C language is problematic * // * error case: ^ [a-zA-Z0-9 ~! @ # $ % ^ & * () _ + '\-= {}\ [\]: \ ";' <> ?,. \/] {6, 20} $ correct case: ^ [] a-zA-Z0-9 ~! @ # $ % ^ & * () _ + '={}:;' <> ?,. /\ "\ [-] {6, 20} $ answer: the most disgusting match has three characters [] -- if it appears at the beginning or end, indicates matching characters '-', such as [^-abc], [-abc], and [abc-]. Note that '\' cannot be used to escape] the first position that can appear in brackets, for example, [] abc] or [^] abc] [escape */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 ("^ [a-zA-Z0-9 ~! @ # $ % ^ & * () _ + '\-= {}\ [\]: \ ";' <> ?,. \\/] + $ "); # Endif # if (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID | CC_TARGET_PLATFORM = CC_PLATFORM_IOS) return StringUtil: StringVerification (strPassword, StringUtil :: format256 ("^ [] a-zA-Z0-9 ~! @ # $ % ^ & * () _ + '={}:;' <> ?,. /\ "\ [-] + $"); # Endif} else if (lengMax! = 0) {# if (CC_TARGET_PLATFORM = CC_PLATFORM_WIN32) return StringUtil: StringVerification (strPassword, StringUtil: format256 ("^ [a-zA-Z0-9 ~! @ # $ % ^ & * () _ + '\-= {}\ [\]: \ ";' <> ?,. \\/] {% D, % d} $ ", lengthMin, lengMax); # endif # if (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID | CC_TARGET_PLATFORM = CC_PLATFORM_IOS) return StringUtil :: stringVerification (strPassword, StringUtil: format256 ("^ [] a-zA-Z0-9 ~! @ # $ % ^ & * () _ + '={}:;' <> ?,. /\ "\ [-] {% 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}
After three days, regular expressions are different in different languages, especially for characters that need to be escaped.
Finally, the regular expression in C ++ 11 is OK, which can be escaped normally.
The Regular Expression in C language cannot be escaped. It must be written in accordance with the standard. "-" is at the end, "]" is at the beginning, and [escape is required.
Reference:
Regex -- use the square brackets POSIX Regular Expression in the regular expression-loverszhaokai