繁体   English   中英

std :: regex_match()冻结我的程序

[英]std::regex_match() freezes my program

所以这是我的程序:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <regex>
#include <windows.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string str = "<link rel=\"shortcut icon\" href=\"http://joyvy.com/img/favicon.png\" />";
    regex expr("<link rel=+(\"|')+shortcut+(.*?(\"|'))+(.*?)href=(\"|')+(.*?)+(\"|')");
    smatch matches;

    cout << "start..." << endl;
    regex_match(str, matches, expr);
    cout << "this will not be printed";
}

这是我的程序的输出:

start...

std :: regex_match()函数调用只是冻结了我的程序。 经过2或3分钟后,它引发错误:

Unhandled exception at at 0x7515B760 in regex.exe: Microsoft C++ exception: std::regex_error at memory location 0x001D9088.

那怎么了

看起来您的正则表达式太复杂了,需要花费很多时间来处理。 造成这种情况的可能原因是您似乎不了解正则表达式中的+含义。 您似乎认为它用于连接或其他操作。 实际上,它的意思是“先前的元素重复了一次或多次”,类似于*意思是“重复了零次或多次”。 删除所有的加号,程序将运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM