话不多说,直接上代码
头文件:
#ifndef __Tools_h__
#define __Tools_h__
#include <string>
class Tools
{
public:
static bool verifyEmailAddress(const std::string& email);
static bool verifyChinese(const std::string& word);
static bool verifyNumber(const std::string& word);
static bool verifyNumberAndEnglish(const std::string& word);
};
#endif // __Tools_h__
源文件:
#include "Tools.h"
#include <regex>
namespace Me
{
bool Tools::verifyEmailAddress(const std::string& email)
{
std::regex pattern("([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)");
return std::regex_match(email, pattern);
}
bool Tools::verifyChinese(const std::string& word)
{
std::regex pattern("^[\u4e00-\u9fa5]+$");
return std::regex_match(word, pattern);
}
bool Tools::verifyNumber(const std::string& word)
{
std::regex pattern("^[0-9]*$");
return std::regex_match(word, pattern);
}
bool Tools::verifyNumberAndEnglish(const std::string& word)
{
std::regex pattern("^[A-Za-z0-9]+$");
return std::regex_match(word, pattern);
}
}