boost之regex使用范例

#include <regex>
#include <list> 

 对wchar_t*字符串的search调用方法:

boost::wcmatch match;

boost::wregex reg(L"\\d{3}你");
boost::regex_search(L"345你好啊!", match, reg);
bool mt0 = match[0].matched;
bool mt1 = match[1].matched;
bool mt2 = match[2].matched;
int ln = match.length();

 

 对std::wstring调用search方法

boost::wsmatch match;
std::wstring str(L"1、运行IVM解器安装程序\
ivmdecoder1.7.1.cn.exe\
2、直接使用鼠标左键双击ivm视频文件即可观看");
boost::wregex reg(L"\\w+(\\d\\.\\d\\.\\d)(\\.\\w+\\.\\w+)");
boost::regex_search(str, match, reg);
int match_size = match.size();
bool mt0 = match[0].matched;
bool mt1 = match[1].matched;
bool mt2 = match[2].matched;
bool mt3 = match[3].matched;
int ln = match.length();
std::wstring s1 = match[0];
std::wstring s2 = match[1];
std::wstring s3 = match[2];

std::wstring s4 = match[3]; 

 因为表达式里有两个分组,所以加上整个字符串共有3个match

 最后一个也就是match[3]是没有值的

 

 对std::wstring调用match方法:

boost::wsmatch match;
boost::wregex reg(L"[^\\d]+(\\d{4})[^\\d]+");
std::wstring ws(L"大家4578好,哈哈!");
bool match_status = boost::regex_match(ws, match, reg);
if (match_status) {

 

对std::wstring 调用replace方法

boost::wregex reg(L"\\d{3}你",
boost::wregex::icase|boost::wregex::perl);
std::wstring str(L"345你好啊!");

 str=boost::regex_replace(str, reg, L"不"); 

 

对std::wstring调用split方法,得到多个std::wstring

std::wstring ws1;
std::list<std::wstring> list;
boost::wregex e(L";|\\|",
             boost::regex::normal | boost::regbase::icase);
std::wstring str = L"你好;他好;我好;哈哈|来吧";
boost::regex_split(std::back_inserter(list), str, e);
std::wstring s;
while(list.size())
{
s = *(list.begin());
list.pop_front();
//cout << s << endl;

你可能感兴趣的:(regex)