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

原文链接: https://www.cnblogs.com/wangshi/archive/2011/11/09/2243702.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Linux C++编程中的正则表达式使用范例

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/36080

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午12:52
下一篇 2023年2月8日 下午12:52

相关推荐