使用MSVC 2013正则表达式错误 [英] Regex error using MSVC 2013

查看:147
本文介绍了使用MSVC 2013正则表达式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块xml像代码来解析,在MSVC 2013中使用std :: regex

 < GLVertex& 
#version 450 core
layout(location = 0)in vec3 pos;
in VertexInfo {
vec2 uv;
} vertexInfo;
void main(){
gl_Position = vec4(pos,1.0);
vertexInfo.uv = pos.xy;
}
< GLVertex />
< GLFragment>
#version 450 core
layout(location = 0)uniform sampler2D g_map;
uniform uniform {
vec4 color;
};
layout(location = 0)out vec4 fragColor;
void main(){
fragColor = texture(g_map,vertexInfo.uv);
}
< GLFragment />

以下是模式:

 < GLVertex>((。| \\\
)+)< GLVertex\ />

但程序总是崩溃!在我的正则表达式有任何错误?我在regex101测试。



PS。当我删除第五行:

  vec2 uv; 

它工作正常!

方案

由于模式不高效,你会得到堆栈溢出(参数:0x00000001,0x00312FFC)异常。我认为它是如何 std :: regex 处理重复的组(你已经定义一个 + - 量化组(。| \\\
)+
)。此模式匹配不是换行符()或换行符( \\\
)的每个字符,然后将该匹配存储在缓冲区中。然后,只有在调试模式中,才会发生迭代器调试问题 std :: _ Orphan_Me 是发生断点的地方,在匹配字符串时,它被认为是最昂贵的方法。请参见


I have a piece of xml like code to parse, using std::regex in MSVC 2013

<GLVertex>
#version 450 core
layout(location = 0) in vec3 pos;
in VertexInfo{
    vec2 uv;
}vertexInfo;
void main(){
    gl_Position = vec4(pos, 1.0);
    vertexInfo.uv = pos.xy;
}
<GLVertex/>
<GLFragment>
#version 450 core
layout(location = 0) uniform sampler2D g_map;
uniform Color {
    vec4 color;
};
layout(location = 0) out vec4 fragColor;
void main(){
    fragColor = texture(g_map, vertexInfo.uv);
}
<GLFragment/>

Here is the pattern:

<GLVertex>((.|\n)+)<GLVertex\/>

But the program always crash! Is there any bug in my regex? I've tested on regex101.

PS. when i delete the 5th line:

vec2 uv;

it works OK!

解决方案

You get a Stack overflow (parameters: 0x00000001, 0x00312FFC) exception as the pattern is not efficient. I think it is related to how std::regex processes repeated groups (you have define one with a +-quantifier group (.|\n)+). This pattern matches each and every character that is not a newline (.), or a newline (\n), and then stores the match inside a buffer. Then, an issue with the iterator debugging happens only in Debug mode. The std::_Orphan_Me is the place where the break occurs, and it is considered the most "expensive" method when matching strings. See performance killer -Debug Iterator Support in Visual studio

You should either switch to the Release mode, or test with a regex that does not require the use of the repeated groups, like any non-null character [^\x00] with lazy quantifier *?:

std::string str1 = "<GLVertex>\n#version 450 core\nlayout(location = 0) in vec3 pos;\nin VertexInfo{\n    vec2 uv;\n}vertexInfo;\nvoid main(){\n    gl_Position = vec4(pos, 1.0);\n    vertexInfo.uv = pos.xy;\n}\n<GLVertex/>\n<GLFragment>\n#version 450 core\nlayout(location = 0) uniform sampler2D g_map;\nuniform Color {\n    vec4 color;\n};\nlayout(location = 0) out vec4 fragColor;\nvoid main(){\n    fragColor = texture(g_map, vertexInfo.uv);\n}\n<GLFragment/>"; 
std::regex reg1("<GLVertex>([^\\x00]*?)<GLVertex/>");
std::smatch find1;
if (std::regex_search(str1, find1, reg1)){
    std::cout << find1[1].str();
}

这篇关于使用MSVC 2013正则表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆