我的 XML 标签正则表达式不起作用

my Regex for xml tags is not working

本文关键字:不起作用 正则表达式 标签 XML 我的      更新时间:2023-10-16
regex starttag("(.*)<w*>");
regex endtag("(.*)</w*>");
regex taganddata(".*<w*>w*</w*>");

这就是我所拥有的,但在尝试匹配时它不起作用,我不知道为什么。

this is a start tag: " <House>"
this is a end tag: " </House>"
This is a data tag: " <City>Allentown</City>"

此外,要匹配的字符串是 xml 文件的一行(因此它在缩进的开头有空格(,因为我正在逐行进行。

我使用了正则表达式和其他服务,看起来它在那里匹配,但不在我的使用 regex stl 的 c++ 程序中

这是不起作用的

if (regex_match(line, starttag))
{
    cout << "Start tag" << endl;
}
if (regex_match(line, endtag))
{
    cout << "End tag" << endl;
}
if (regex_match(line, taganddata))
{
    cout << "Data and Tags" << endl;
}

您的正则表达式需要正确转义。 即正斜杠需要正确转义

regex starttag("(.*)<\w*>");
regex endtag("(.*)<\/\w*>");
regex taganddata(".*<\w*>\w*<\/\w*>");

或者,您可以使用原始字符串格式:

regex starttag(R"((.*)<w*>)");
regex endtag(R"((.*)</w*>)");
regex taganddata(R"(.*<w*>w*</w*>)");