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++ 使用c++/boost::regex_C++_Regex_String_Boost - Fatal编程技术网

C++ 使用c++/boost::regex

C++ 使用c++/boost::regex,c++,regex,string,boost,C++,Regex,String,Boost,我有一系列数千个HTML文件,为了运行词频计数器,我只对每个文件的特定部分感兴趣。例如,假设以下内容是其中一个文件的一部分: <!-- Lots of HTML code up here --> <div class="preview_content clearfix module_panel"> <div class="textelement "><div><div><p><em>"Portio

我有一系列数千个HTML文件,为了运行词频计数器,我只对每个文件的特定部分感兴趣。例如,假设以下内容是其中一个文件的一部分:

<!-- Lots of HTML code up here -->
<div class="preview_content clearfix module_panel">
      <div class="textelement   "><div><div><p><em>"Portion of interest"</em></p></div>
</div>
<!-- Lots of HTML code down here -->

“利息部分”

我应该如何在C++中使用正则表达式(Boosi::ReGEX)来提取示例中突出显示的文本的特定部分,并将其放入单独的字符串?

目前,我有一些代码可以打开html文件并将整个内容读入单个字符串,但当我尝试运行
boost::regex_match
查找行的特定开头时,我没有得到任何匹配。我对任何建议开放,只要它是在C++上。

我应该如何在C++中使用正则表达式(Boosi::ReGEX)来提取示例中突出显示的文本的特定部分,并将其放入单独的字符串?

你没有

永远不要使用正则表达式来处理HTML。无论是在C++中使用Boosix.ReEX,在Perl、Python、JavaScript、任何地方都可以。HTML不是一种常规语言;因此,它不能通过正则表达式以任何有意义的方式进行处理。哦,在极为有限的情况下,您可能可以让它提取一些特定的信息。但一旦这些情况发生变化,你就会发现自己无法完成你需要完成的事情


我建议使用实际的HTML解析器,比如(它确实能够读取HTML4)。但是使用regex解析HTML只是使用了错误的工具。

因为我所需要的只是一些非常简单的东西(根据上面的问题),所以我能够在不使用regex或任何类型的解析的情况下完成它。以下是实现此技巧的代码片段:

    // Read HTML file into string variable str
    std::ifstream t("/path/inputFile.html");
    std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());

    // Find the two "flags" that enclose the content I'm trying to extract
    size_t pos1 = str.find("<div class=\"preview_content clearfix module_panel\">");
    size_t pos2 = str.find("</em></p></div>");

    // Get that content and store into new string
    std::string buf = str.substr(pos1,pos2-pos1);
//将HTML文件读入字符串变量str
std::ifstream t(“/path/inputFile.html”);
std::string str((std::istreambuf_迭代器(t)),std::istreambuf_迭代器();
//找到包含我试图提取的内容的两个“标志”
大小\u t pos1=str.find(“”);
大小\u t pos2=str.find(“

”); //获取该内容并存储到新字符串中 std::string buf=str.substr(pos1,pos2-pos1);

谢谢你指出我完全走错了方向。

强制性参考:是的,在读了更多的书之后,我现在发现在处理html时使用正则表达式似乎不是个好主意。谢谢你指出这一点。考虑到这是我唯一想做的事情(即获取特定标签的内容),您建议我使用什么?我一直在四处寻找,但我遇到的大多数事情似乎都有点过头了。