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

时间:2013-07-17 12:34:39

标签: c++ regex visual-c++ runtime-error std

所以这是我的计划:

#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.

那有什么不对?

1 个答案:

答案 0 :(得分:2)

看起来你的正则表达式太复杂了,需要永远处理。可能的原因是你似乎不理解+在正则表达式中的含义。你似乎相信它用于串联或其他东西。实际上,它意味着“前一个元素重复一次或多次”,类似于*,意思是“重复零次或多次”。删掉所有的优点,程序就可以了。

相关问题