您的位置:首页 > 其它

常用的正则表达式

2016-06-27 08:56 309 查看
话不多说,直接上代码

头文件:

#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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  正则表达式