正则表达式2-测试代码

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include"stdio.h"
#include<iostream>
#include<boost/xpressive/xpressive.hpp>
using namespace boost::xpressive;
using namespace std;
int main(int argc, char* argv[]) 
{ 
	cout << "------------regex_match----------" << endl;
	cregex reg = cregex::compile(".*(\\d{8}T\\d{6}).");
	char p[] = "/opt/myapp/bin/result/20180101T081211N";
	cmatch what;
	if (regex_match(p,what, reg)) //要求字符串完全匹配
	{
		cout << "ok" << endl;
		assert(what.size() == 2);
		cout << what[0] << " " << what[1] << endl;
	}
	else {
		cout << "fail" << endl;
	}
	cout << "------------regex_search----------" << endl;
	char s[] = "/opt/myapp/bin/result/20180101T081211N";
	cregex reg2 = cregex::compile("(\\d+T\\d+).");
	if (regex_search(s, what, reg2)) //
	{
		cout << "ok" << endl;
		assert(what.size() == 2);
		cout << what[0] << " " << what[1] << endl;
	}
	else {
		cout << "fail" << endl;
	}

	cout << "------------sregex_iterator----------" << endl;


	std::string str = "my name is cc and age is 27 and birthday is 0812";
	sregex sreg = sregex::compile("\\d+");
	sregex_iterator it(str.begin(), str.end(), sreg);//返回一个matach_results对象
	sregex_iterator end;
	for (; it != end; it++) {
		cout << (*it)[0] << " ";
	}
	cout << endl;
	cout << "------------sregex_token_iterator----------" << endl;

	sregex_token_iterator token(str.begin(), str.end(), sreg,-1);//返回一个sub_match对象,-1正则表达式作为分隔符,默认匹配字符串
	sregex_token_iterator token_end;
	for (; token != token_end; token++) {
		cout << (*token)<< " ";
	}
	cout << endl;
	system("pause"); return 0; 
}

发布了136 篇原创文章 · 获赞 22 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/u010261063/article/details/86513297