正则表达式使用范例 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;
}
基本介绍:
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;
}
相关文章推荐
- Linux下正则表达式的使用及grep工具
- Linux正则表达式的使用和基本命令grep、egrep、fgrep的用法
- Linux企业级项目实践之网络爬虫(14)——使用正则表达式抽取HTML正文和URL
- Python正则表达式的使用范例详解
- linux下使用正则表达式修改文件内容
- linux 命令 grep 不使用 正则表达式
- 使用Linux正则表达式灵活搜索文件中的文本
- Linux正则表达式使用指南
- Python正则表达式的七个使用范例
- 正则表达式使用学习(C++、Qt、Python)
- Linux企业级项目实践之网络爬虫(14)——使用正则表达式抽取HTML正文和URL
- 在C/C++语言中使用正则表达式
- Python正则表达式的七个使用范例
- [C/C++标准库]_[初级]_[使用正则表达式过滤Windows文件名中的非法字符]
- Linux正则表达式相关知识及命令的使用方法
- Linux 系统中正则表达式的使用
- JavaScript正则表达式语法及使用范例大全
- 基于Linux下详解正则表达式(基本正则和扩展正则命令使用实例)
- linux下的C中正则表达式的使用
- linux 正则表达式 使用简单演示