封装常用正则表达式的用法

这篇文章主要介绍了使用C++封装常用正则表达式的用法,方便以后直接使用,最后还给出了测试代码,大家可运行测试使用

regexhelper.h

复制代码 代码如下:

#ifndef REGEX_HELPER_H_INCLUDE
#define REGEX_HELPER_H_INCLUDE
#include
#include
namespace Framework{
class RegexHelper
{
public:
    RegexHelper();
    virtual ~RegexHelper();
    /*
    * 是否包含匹配字符串
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    */
    static bool IsMatch(const char* input,const char* pattern);
    /*
    * 获取首个匹配字符串或其字串
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    * @param:group 子捕获组
    */
    static std::string Match(const char* input,const char* pattern,int group = 0);
    /*
    * 获取首个匹配字符串所有捕获组
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    * @param: results 输出的字符串数组
    */
    static int Match(const char* input,const char* pattern,std::vector& results);
    /*
    * 匹配字符串数目
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    */
    static int Matches(const char* input,const char* pattern);
    /*
    * 输出所有匹配字符串或其捕获组
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    * @param: results 输出的字符串数组
    * @param:group 捕获组
    */
    static int Matches(const char* input,const char* pattern,std::vector& results,int group = 0);
    /*
    * 替换首个匹配字符串
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    * @param:repValue 被替换值,可以是捕获组的组合
    */
    static std::string ReplaceFirst(const char* input,const char* pattern,const char* repValue);
    /*
    * 替换所有匹配字符串
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    * @param:repValue 被替换值,可以是捕获组的组合
    */
    static std::string ReplaceAll(const char* input,const char* pattern,const char* repValue);
    /*
    * 分割字符串并输出结果
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    * @param: results 输出的字符串数组
    */
    static int Split(const char* input,const char* pattern,std::vector& results);
    /*
    * 分割字符串并根据捕获组输出
    * @param: input 输入字符串
    * @param:pattern 正则表达式
    * @param:subs 捕获组
    * @param: results 输出的字符串数组
    */
    static int Split(const char* input,const char* pattern,std::vector& subs,std::vector& results);

protected:
private:
};
}
#endif // REGEX_HELPER_H_INCLUDE

以上就是封装常用正则表达式的用法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言