【发布时间】:2015-09-04 20:14:32
【问题描述】:
我一直在尝试一些看似相当简单的事情……一开始。
我正在尝试使用正则表达式转换我一直在使用 strstr 进行的文本操作,这似乎是现在使用 c++11 的方式。这是一个测试用例示例:
<!Sometag>
// Lots of code here! (Multiline)
<Sometag!>
<!Sometag2>
// Lots of code here! (Multiline)
<Sometag2!>
编辑:一个更明确的例子。
/// Comments.
<!Vertex>
#version 150
/// code here!
void main()
{
/// code here!
}
<Vertex!>
/// Comments.
<!Fragment>
#version 150
/// code here!
void main()
{
/// code here!
}
<Fragment!>
编辑 2:这是一个更好的例子来说明需要做什么:
我已经做了很多组合,但想到的最合乎逻辑的是这个:
std::smatch u;
std::string s = shader->GetData();
std::regex_match(s, u, std::regex("<.*>(.*)<!.*>"));
我还没有运气,我想知道是否有人知道语法可能是什么?!
谢谢
【问题讨论】:
-
和 !对正则表达式来说是特殊的。你需要逃离他们。转义字符是 C 字符串特有的 \,你也需要转义它们,所以试试 "\\]*\\>(.*)\\]\\> " - 不过我现在没有办法测试它
-
我猜缺少一个 *,我尝试了这个以及您的建议:"\\]*\\>(.*)\\]*\\>" 到目前为止还没有运气。我会继续挖掘。
-
您可以使用原始字符串文字代替转义,例如
std::regex(R"delim(<.*>(.*)<!.*>)delim")。请参阅#6 here 或solarianprogrammer.com/2011/10/16/…。 -
谢谢vsoftco,已经注意到了。
-
您确定要使用 regex_match,也许 regex_search 更合适? match 必须匹配整个目标