Use the C ++ 11 Regular Expression in cocos2dx and the C language Regular Expression in android -------- case password verification, cocos2dxandroid

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.