---
title: "C++正则表达式匹配、分割字符串"
author: "7090"
date: "2017-11-14T16:32:33Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2017-11-14T16:32:33Z"
id: 2399703
url: "https://7090.iteye.com/blog/2399703"
---

#include "stdafx.h"
#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main()
{

	std::string s("abcd12_efgh34");
	std::regex e("abcd(\\d+)_efgh(\\d+)");
	std::smatch sMatchResult;

	if (std::regex_match(s, sMatchResult, e))
	{
		std::cout << "string Match:" << sMatchResult.size() << "\n";

		for (size_t i = 0; i < sMatchResult.size(); ++i)
		{
			cout << sMatchResult* << " ";
		}
	}
	cout << "\n";

	std::cmatch cMatchResult;    // same as std::match_results<const char*> cm;
	if (std::regex_match(s.c_str(), cMatchResult, e))
	{
		std::cout << "char* Match:" << cMatchResult.size() << "\n";

		for (size_t i = 0; i < cMatchResult.size(); ++i)
		{
			cout << cMatchResult* << " ";
		}
	}

	getchar();

	std::cout << std::endl;
}


参考：http://www.cplusplus.com/reference/regex/regex_match/