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");
regfree(®);
if (retval == 0) {
return true;
}
else
{
return false;
}
#endif
}
測試用例:
StringUtil::StringVerification(strPhoneNum, "^1\d{10}$");
跪求高手解答,為什麼C語言的通不過呢???
try:
^1[0-9]{10}$