Boost::Regex使用

    技术2022-05-19  24

    背景     

         Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一。 Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容。在C++社区中影响甚大,是不折不扣的“准”标准库。

    Boost由于其对跨平台的强调,对标准C++的强调,用其来进行编写平台无关的应用,是非常有帮助的。大部分boost库功能的使用只需包括相应头文件即可,少数(如正则表达式库,文件系统库等)需要链接库。在Boost库中,Regex也就是正则表达式库正是被众多C++爱好者大加赞叹的部分。

    Boost的详细信息请到其官网www.boost.org

    Boost::Regex

         下面就简单的介绍一下Boost中Regex的使用。

          正则表达式在文本处理方面非常强大,通过对正则表达式的支持,各种语言在文本处理能力上展示强大的功能。比较常见的Perl语言,几乎可以在每个Perl程序中都见到正则表达式的身影。当然,文本处理本身就是Perl的卖点。

         Boost库中对Regular Expression包含在Boost::Regex中,关于Boost的安装配置请Google吧,很多图文并茂的教程。

         Boost::Regex其实实现的就是正则表达式的基本应用:匹配,查找,替换。

    boost::regex_match

        先说说匹配boost::regex_match,其主要功能就是验证当期的字符串是否符合模式定义的情况,具体请看下面的程序片段:

    #include <stdlib.h> #include <boost/regex.hpp> #include <string> #include <iostream>

    boost::regex expression("([0-9]+)(//-| |$)(.*)");

    //这个正则表达式是“220-xxxxxxx” 或者“220 xxxxxx” 或者 “220” 这样的内容

    //简单讲,就是找找看字符串是不是以数字开头的。

    // process_ftp: // on success returns the ftp response code, and fills // msg with the ftp response message. int process_ftp(const char* response, std::string* msg) {    boost::cmatch what;    if(boost::regex_match(response, what, expression))    {       // what[0] contains the whole string       // what[1] contains the response code       // what[2] contains the separator character       // what[3] contains the text message.

         // 这里需要讲一下这个what,它是match_results的一类,如果当前比配是成功的,那么what里面的内容请参考

        //boost::match_result中的定义,如果匹配不成功,那么what是未定义的

          if(msg)          msg->assign(what[3].first, what[3].second);       return std::atoi(what[1].first);    }    // failure did not match    if(msg)       msg->erase();    return -1; }

     

    boost::regex_search

     boost::regex_search,,其主要功能是在当前字符串中查找符合模式的子串。请参考下面的程序片段

    #include <string> #include <map> #include <boost/regex.hpp>

    // purpose: // takes the contents of a file in the form of a string // and searches for all the C++ class definitions, storing // their locations in a map of strings/int's typedef std::map<std::string, int, std::less<std::string> > map_type;

    boost::regex expression(    "^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"    "(class|struct)[[:space:]]*"    "(//<//w+//>([[:blank:]]*//([^)]*// ))?"    "[[:space:]]*)*(//<//w*//>)[[:space :]]*"    "(<[^;:{]+>[[:space:]]*)?(

    void IndexClasses(map_type& m, const std::string& file) {    std::string::const_iterator start, end;    start = file.begin();    end = file.end();       boost::match_results<std::string::const_iterator> what;    boost::match_flag_type flags = boost::match_default;    while(regex_search(start, end, what, expression, flags))    {       // what[0] contains the whole string       // what[5] contains the class name.       // what[6] contains the template specialisation if any.       // add class name and position to map:       m[std::string(what[5].first, what[5].second)             + std::string(what[6].first, what[6].second)]          = what[5].first - file.begin();       // update search position:       start = what[0].second;       // update flags:       flags |= boost::match_prev_avail;       flags |= boost::match_not_bob;    } }

    boost::regex_replace

    boost::regex_replace,其主要功能是将当前字符串中符合模式的子串替换成定义好的子串,不改变字符串中其他字符的情况。请参考下面的程序片段

     std::string testString = "This is a wonderfull show and great game";     std::string expstring = "(g)";     boost::regex expression(expstring);  const char* format_string = "G";

     testString = boost::regex_replace( testString, expression,format_string );  std::cout<< "TrimLeft:" << testString <<std::endl;

     

    关于boost::regex_replace还有很多方便的用法,其功能及其强大,简单到十多个语句就能完成文档替换输出等功能。

    因篇幅的关系,等有时间再一一展开吧。


    最新回复(0)