gpt4 book ai didi

c++ - std::regex_match() 卡住了我的程序

转载 作者:行者123 更新时间:2023-11-30 04:16:33 28 4
gpt4 key购买 nike

这是我的程序:

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

怎么了?

最佳答案

看起来您的正则表达式太复杂了,需要很长时间才能处理。可能的原因是您似乎不理解 + 在正则表达式中的含义。您似乎认为它用于连接或其他用途。其实就是“前面的元素重复了一次或多次”的意思,类似于*的意思是“重复了零次或多次”。去掉所有加号,程序运行。

关于c++ - std::regex_match() 卡住了我的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17699901/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com