您的位置:首页 > 运维架构 > Linux

正则表达式使用范例 Linux C++

2012-06-11 19:31 597 查看
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(®ex, pattern.c_str(), REG_EXTENDED | REG_NOSUB);
char errbuf[128];
if (errcode != 0)
{
regerror(errcode, ®ex, 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(®ex, txt1.c_str(), 0, NULL, eflags);
assert(0 == errcode); //match success
errcode = regexec(®ex, txt2.c_str(), 0, NULL, eflags);
assert(REG_NOMATCH == errcode); //match fail
regfree(®ex);

//extract sub-pattern
errcode = regcomp(®ex, pattern.c_str(), REG_EXTENDED);
regmatch_t value[3];
errcode = regexec(®ex, 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(®ex);

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