#include <iostream>
#include <string>
#include <regex>
using namespace std;
/*
标题:VS2019-std::regex功能测试
最后更新日期:2020-8-28
作者: kagula
环境:VS2019 Community version 16.7.1
注意下面两个方法的区别:
regex_match匹配整个源, regex_search匹配源的一部份。
待改进的地方:
[1]下次有机会, 改成VS单元测试形式.
备注:
[1]建立正则表达式后, 在下面网站(PCRE-PHP)测试通过后, 再移植到C++中.
https://regex101.com/
注意/符号在网站中需要转义, 但是在C++中不需要转义.
*/
int main(int argc, char* argv[])
{
try
{
cout << endl << "示例:匹配整个源,Expression中只有一个capture" << endl;
std::string hello("<img src=\"http://localhost:8080/a.jpg\">");
//<(?:img|IMG)[\s\S]* src="{0,2}(\S+[^"/ ])"{0,2}[\s\S]*/{0,1}>
std::regex rex("<(?:img|IMG)[\\s\\S]*src=\"{0,2}(\\S+[^\"\/ ])\"{0,2}[\\s\\S]*\\/{0,1}>");
std::smatch what;//如果std::string类型替换为boost::smatch
if (std::regex_match(hello, what, rex))
{
cout <<
VS2019-std::regex功能测试
于 2020-08-28 14:59:16 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)