简体   繁体   English

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

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

So here is 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";
}

And here is the output of my program: 这是我的程序的输出:

start...

The std::regex_match() function call is just freezes my program. std :: regex_match()函数调用只是冻结了我的程序。 After the lapse of 2 or 3 minutes it trows error: 经过2或3分钟后,它引发错误:

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

So what's wrong? 那怎么了

Looks like your regular expression is just too complex, and takes forever to process. 看起来您的正则表达式太复杂了,需要花费很多时间来处理。 And the likely reason for that is that you don't seem to understand what + means in a regular expression. 造成这种情况的可能原因是您似乎不了解正则表达式中的+含义。 You seem to believe it's used for concatenation or something. 您似乎认为它用于连接或其他操作。 In fact, it means "the previous element repeated one or more times", similar to * meaning "repeated zero or more times". 实际上,它的意思是“先前的元素重复了一次或多次”,类似于*意思是“重复了零次或多次”。 Drop all the pluses, and the program works. 删除所有的加号,程序将运行。

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

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