gpt4 book ai didi

c++ - 使用 MSVC 2013 的正则表达式错误

转载 作者:行者123 更新时间:2023-11-30 05:28:42 24 4
gpt4 key购买 nike

我有一段类似 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 Color {
vec4 color;
};
layout(location = 0) out vec4 fragColor;
void main(){
fragColor = texture(g_map, vertexInfo.uv);
}
<GLFragment/>

这是模式:

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

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

附言。当我删除第 5 行时:

vec2 uv;

一切正常!

最佳答案

你得到一个堆栈溢出(参数:0x00000001,0x00312FFC)异常,因为模式效率不高。我认为这与 std::regex 处理重复组的方式有关(您已经定义了一个带有 +-量词组 (.|\n)+)。此模式匹配每一个不是换行符 (.) 或换行符 (\n) 的字符,然后将匹配项存储在缓冲区中。然后,迭代器调试问题仅在调试模式下发生。 std::_Orphan_Me 是中断发生的地方,它被认为是匹配字符串时最“昂贵”的方法。参见 performance killer -Debug Iterator Support in Visual studio

您应该切换到Release 模式,或者使用不需要使用重复组的正则表达式进行测试,例如任何非空字符 [^\x00] 带有惰性量词 *?:

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();
}

enter image description here

关于c++ - 使用 MSVC 2013 的正则表达式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686979/

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