zoukankan      html  css  js  c++  java
  • Linux C++编程中的正则表达式使用范例

    POSIX正则库使用
    基本介绍:
    POSIX(Portable Operating System Interface of Unix) 是unix系统提供的系统级通用正则库。
    四个主要接口:regcomp, regexec, regerror, regfree (可以通过man命令查询参数含义)
     
    代码示例:
    #include<iostream>
    #include<string>
    #include<sys/types.h>
    #include<regex.h>
    #include<assert.h>

    using namespace std;

    int main(int argc, char** argv)
    {
        string pattern("([[:alnum:]]+):([[:digit:]]+)"); // ([a-z]+):([0-9]+) also works here
        regex_t regex;

        //compile
        int errcode = regcomp(&regex, pattern.c_str(), REG_EXTENDED | REG_NOSUB);
        char errbuf[128];
        if (errcode != 0)
        {
            regerror(errcode, &regex, errbuf, sizeof(errbuf)); //get error info
            cerr << pattern << ": " << errbuf << endl;
        }

        int eflags = 0;
        //partical match
        string txt1 = "ruby:123";
        string txt2 = "ruby:abc";
        errcode = regexec(&regex, txt1.c_str(), 0, NULL, eflags);
        assert(0 == errcode); //match success  
        errcode = regexec(&regex, txt2.c_str(), 0, NULL, eflags);
        assert(REG_NOMATCH == errcode); //match fail
        regfree(&regex);

       //extract sub-pattern
       errcode = regcomp(&regex, pattern.c_str(), REG_EXTENDED);
       regmatch_t value[3];
       errcode = regexec(&regex, txt1.c_str(), 3, value, eflags);
       assert(0 == errcode); //match success  
       string all(txt1.c_str() + value[0].rm_so, txt1.c_str() + value[0].rm_eo);
       string word(txt1.c_str() + value[1].rm_so, txt1.c_str() + value[1].rm_eo); //first sub-pattern
       string num(txt1.c_str() + value[2].rm_so, txt1.c_str() + value[2].rm_eo); //second sub-pattern
       assert("ruby:123" == all);
       assert("ruby" == word);
       assert("123" == num);
       regfree(&regex);

       return 0;
    }
     
    [1] linux posix regex man page. http://linux.die.net/man/3/regex
     
    PCRE正则库使用
    基本介绍:
    PCRE(Perl Compatible Regular Expressions)是一个用C语言编写的开源轻量级正则表达式函数库,PCRE也是perl语言的缺省正则库。
     
    代码示例:
    #include<iostream>
    #include<string>
    #include<pcrecpp.h>
    #include<assert.h>

    using namespace std;

    int main(int argc, char** argv)
    {
        pcrecpp::RE re("(\\w+):(\\d+)");
        string txt1 = "ruby:123";
        string txt2 = "ruby:123s";

        //full match
        assert(true == re.FullMatch(txt1));
        assert(false == re.FullMatch(txt2));

       //partical match
       assert(true == re.PartialMatch(txt2));

       //extract sub-patterns
       int num = 0;
       string str;
       re.FullMatch(txt1, &str, &num);
       assert("ruby" == str);
       assert(123 == num);
       return 0;
    }
     
    编译command: g++ -o source source.cpp -I /usr/local/include/pcre/ -L /usr/local/lib/pcre/ -lpcrecpp
     
    [1]pcre download and install. http://www.pcre.org/
    [2]linux pcrecpp man page. http://linux.die.net/man/3/pcrecpp
     
    Boost Regex正则库使用
    基本介绍:
    Boost库是一个可移植、提供源代码的C++库,作为标准库的后备。Boost库pcre的封装版本。
     
    #include<iostream>
    #include<string>
    #include<assert.h>
    #include<boost/regex.hpp>

    using namespace std;

    int main(int argc, char** argv)
    {
        boost::regex re("(\\w+):(\\d+)");

        //full match
        string txt1 = "ruby:123";
        string txt2 = "ruby:123s";
        assert(true == boost::regex_match(txt1, re));
        assert(false == boost::regex_match(txt2, re));
     
        //partial match
        assert(true == boost::regex_search(txt2, re));

       //extract sub-pattern

       boost::match_results<string::const_iterator> what;
       if (regex_match(txt1, what, re, boost::match_default))
       {
           assert("ruby" == what[1]);
           assert("123" == what[2]);
       }

       return 0;
    }
     
    编译command: g++ -o source source.cpp -lboost_regex

  • 相关阅读:
    最近吸收的html && CSS 知识
    Visual C++ 20111021
    递归变位数(练习)
    effective C++ 第五章
    二叉树的非递归遍历
    插入排序补充
    数的乘方,简单背包,组合
    在这个病毒猖獗的年代……
    元宵夜游城隍庙
    Cherish your work
  • 原文地址:https://www.cnblogs.com/wangshi/p/2243702.html
Copyright © 2011-2022 走看看