基本介紹:
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-patternlinux
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;
}
ios