Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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++ 如何使用fscanf提取html_C++_Scanf - Fatal编程技术网

C++ 如何使用fscanf提取html

C++ 如何使用fscanf提取html,c++,scanf,C++,Scanf,我有一个文件,每行有一个 <div style="random properties" id="keyword1:string id:int">text</div> <div style="random properties" id="keyword1:string id:int">text</div> <div style="random properties" id="keyword2:string id:int">text<

我有一个文件,每行有一个

<div style="random properties" id="keyword1:string id:int">text</div>
<div style="random properties" id="keyword1:string id:int">text</div>
<div style="random properties" id="keyword2:string id:int">text</div>
<div style="random properties" id="keyword2:string id:int">text</div>
文本
文本
文本
文本

我可以使用fscanf返回匹配关键字1和关键字2的文本和id列表吗?

您只需使用regex读取即可:

std::string s;
std::regex r( "<div style=\"[^\"]*\" id=\".*(\\d+)\">((?:(?!</div>).)*)</div>" );
while( std::getline(in, s) ) {
    std::smatch m;
    if( std::regex_match(s, m, r) ) {
        std::cout << "id = " << m.str(1) << ", text = " << m.str(2) << std::endl;
    } else {
        std::cout << "invalid pattern" << std::endl;
    }
}
std::字符串s;

std::regex r(“是的,你可以。但是如果你使用html解析库,甚至像yacc这样的解析器生成器,你也会保持理智。你有没有理由特别想使用
fscanf
?这有点像……有很多现有的xml解析器,例如tinyXml。你也可以使用正则表达式(boost和c++11标准库支持它们)或者使用boost spirit创建您自己的解析器。为什么您要坚持使用fscanf,因为它有这么多问题…好吧,不管是什么解决方案,都有1000行我需要的信息,而我以前尝试的唯一一件事是fscanf,我只是无法让它工作。我只是将该行放入fscanf中,并用%s和id%d重新绘制文本-无需重新绘制Sults。我有C和C编译器,而不是寻找设置YACC,除非它很容易。假设这不是一个真正的HTML文件,每一行实际上是开始和结束的。你是正确的,但是听起来他/她想要一种方式来做它。一个解决方案,像这里的一个可以工作,但不是脆弱的东西,如新行的中间。DIV标签或格式更改@他/她需要一个HTML解析库来解析完整的HTML,并且没有办法,也不需要有一个不太熟悉C++和HTML的人可以为它编写解析器,所以我认为这将帮助他/她至少学习强大的<代码> ReGEX < /C>并开始使用它进行简单的解析!但是如果他/她如果需要一种方法来解析完整的HTML,或者输入的格式可能会改变,那么他/她应该开始使用HTML解析器库