Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;std::regex如何修复错误\u复杂性?_Regex_C++11 - Fatal编程技术网

C++;std::regex如何修复错误\u复杂性?

C++;std::regex如何修复错误\u复杂性?,regex,c++11,Regex,C++11,我使用std::regex来匹配字符串 我对regex的定义是: regex reg("(-?\\d+,?){2,}", regex::icase) 测试字符串为: 5,3240,7290,11340,-3240,-7290,-11340 我使用了regex\u match()的std函数。 下面是我得到的错误 regex_error(error_complexity):尝试匹配的复杂性 对正则表达式的访问超过了预设级别 我怎样才能解决这个问题?我的编译器是VS2013。您可以将包含,?的组

我使用
std::regex
来匹配字符串

我对regex的定义是:

regex reg("(-?\\d+,?){2,}", regex::icase)
测试字符串为:

5,3240,7290,11340,-3240,-7290,-11340
我使用了
regex\u match()
的std函数。 下面是我得到的错误

regex_error(error_complexity):尝试匹配的复杂性 对正则表达式的访问超过了预设级别

我怎样才能解决这个问题?我的编译器是VS2013。

您可以将包含
,?
的组“展开”为更线性的模式,以降低复杂性-
,?-?\\d+(?:,-?\\d+”

见:

#包括
#包括
使用名称空间std;
int main(){
正则表达式reg(“,?-?\\d+(?:,-?\\d+”);
字符串s(“53240729011340,-3240,-7290,-11340”);
如果(正则表达式匹配,正则表达式匹配){

std::您的正则表达式可以写为
“[xyz]-?\\d+(?:,[xyz]-?\\d+”
,但它与您提供的字符串不匹配,因为其中没有
x
y
z
。可能您需要
“[xyz]-?-?\\d+(?:,[xyz]?-?-?-?\\d+”
?对不起,我放错了正则表达式。它已被修改。同样,它包含嵌套的量词,其中一个模式是必需的,另一个是可选的。首先,将其更改为
-?\\d+(?:,-?\\d+)
,然后切换到释放模式。我不知道为什么它不匹配开头带有逗号的字符串,就像这样(,32407229011340)。它达到了我的目的。我只想知道为什么。在开始时添加
,?
。我知道。因为我放错了正则表达式。我很抱歉。请再次查看。谢谢。我更新了答案,允许匹配
53240729011340,-3240,-7290,-11340
和。
#include <iostream>
#include <regex>
using namespace std;

int main() {
    regex reg(",?-?\\d+(?:,-?\\d+)+");
    string s("5,3240,7290,11340,-3240,-7290,-11340");
    if (regex_match(s, reg)) {
        std::cout << "Matched!"; 
    }
    return 0;
}