gpt4 book ai didi

c++ - 匹配字符串长度;查找和替换;在 C++ 中删除 HTML 标签

转载 作者:行者123 更新时间:2023-11-27 23:14:57 25 4
gpt4 key购买 nike

这是我的问题
我正在尝试删除 HTML 标签,简单地格式化它们(比如每条信息换行),然后编写其他程序以在以后提取有用的信息。
到目前为止,我已经将数据读取到一个大字符串数组中。
这是我的想法:找到 <> 中的所有标签并将它们替换为空白。上网查了一下,看到了一些可以做查找替换的程序。
因为不同的 HTML 标签有不同的长度,所以我想知道 C++ 中是否有一种方法可以检测任何长度的标签,只要它在 <> 中。然后替换它们。
这是一个 HTML 示例代码

<div style="width: 200px;"><strong>Balance Sheets (USD $)<br></strong></div>

我希望在手术后得到:

Balance Sheets (USD $)


谁能帮忙?或者有没有人有更好的主意来处理这个任务?非常感谢任何帮助!

<DOCUMENT>
<TYPE>XML
<SEQUENCE>33
<FILENAME>R2.htm
<DESCRIPTION>IDEA: XBRL DOCUMENT
<TEXT>
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
<link rel="StyleSheet" type="text/css" href="report.css"><script type="text/javascript" src="Show.js">/* Do Not Remove This Comment */</script></head>
<body><span style="display: none;">v2.4.0.6</span><table class="report" border="0" cellspacing="2" id="ID0EQAAG">
<tr>
<th class="tl" colspan="1" rowspan="1">
<div style="width: 200px;"><strong>Balance Sheets (USD $)<br></strong></div>
</th>
<th class="th">
<div>Dec. 31, 2012</div>
</th>
<th class="th">
<div>Dec. 31, 2011</div>
</th>
</tr>
<tr class="re">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_tpoi_CurrentAssetsAbstract', window );"><strong>Current assets:</strong></a></td>
<td class="text">&#xA0;<span></span></td>
<td class="text">&#xA0;<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_CashAndCashEquivalentsAtCarryingValue', window );">Cash and cash equivalents</a></td>
<td class="nump">$ 106,999<span></span></td>
<td class="nump">$ 52,109<span></span></td>
</tr>
<tr class="re">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AccountsReceivableNetCurrent', window );">Accounts receivable</a></td>
<td class="nump">110,720<span></span></td>
<td class="nump">61,218<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_tpoi_AccountsReceivableRelatedParty', window );">Accounts receivable - related party</a></td>
<td class="nump">1,527<span></span></td>
<td class="text">&#xA0;<span></span></td>
</tr>
<tr class="reu">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AssetsCurrent', window );">Total current assets</a></td>
<td class="nump">219,246<span></span></td>
<td class="nump">113,327<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_PropertyPlantAndEquipmentNet', window );">Property and equipment, net</a></td>
<td class="nump">152,724<span></span></td>
<td class="nump">160,454<span></span></td>
</tr>
<tr class="re">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_CapitalizedSoftwareDevelopmentCostsForSoftwareSoldToCustomers', window );">Capitalized software development costs</a></td>
<td class="nump">188,371<span></span></td>
<td class="text">&#xA0;<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_IntangibleAssetsNetExcludingGoodwill', window );">Intangible assets, net</a></td>
<td class="nump">59,151<span></span></td>
<td class="nump">59,151<span></span></td>
</tr>
<tr class="re">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_OtherAssetsCurrent', window );">Other assets</a></td>
<td class="nump">11,622<span></span></td>
<td class="nump">15,470<span></span></td>
</tr>
<tr class="rou">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_NoncurrentAssets', window );">Total long term assets</a></td>
<td class="nump">411,868<span></span></td>
<td class="nump">235,075<span></span></td>
</tr>
<tr class="reu">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_Assets', window );">Total assets</a></td>
<td class="nump">631,114<span></span></td>
<td class="nump">348,402<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_tpoi_CurrentLiabilitiesAbstract', window );"><strong>Current liabilities:</strong></a></td>
<td class="text">&#xA0;<span></span></td>
<td class="text">&#xA0;<span></span></td>
</tr>
<tr class="re">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AccountsPayableCurrent', window );">Accounts payable</a></td>
<td class="nump">50,866<span></span></td>
<td class="nump">70,253<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AccruedLiabilitiesCurrent', window );">Accrued liabilities</a></td>
<td class="nump">1,452<span></span></td>
<td class="nump">6,752<span></span></td>
</tr>
<tr class="reu">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_Liabilities', window );">Total current liabilities</a></td>
<td class="nump">52,318<span></span></td>
<td class="nump">77,005<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_OtherAccruedLiabilitiesNoncurrent', window );">Other non current liabilities, accrued interest</a></td>
<td class="nump">7,500<span></span></td>
<td class="nump">1,500<span></span></td>
</tr>
<tr class="re">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_NotesPayableCurrent', window );">Notes payable</a></td>
<td class="nump">50,000<span></span></td>
<td class="nump">50,000<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_DueToRelatedPartiesCurrent', window );">Notes payable - related party</a></td>
<td class="nump">100,000<span></span></td>
<td class="nump">100,000<span></span></td>
</tr>
<tr class="reu">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_OtherLiabilitiesCurrent', window );">Total long-term liabilities</a></td>
<td class="nump">157,500<span></span></td>
<td class="nump">151,500<span></span></td>
</tr>
<tr class="rou">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_LiabilitiesCurrent', window );">Total liabilities</a></td>
<td class="nump">209,818<span></span></td>
<td class="nump">228,505<span></span></td>
</tr>
<tr class="re">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_tpoi_ShareholdersEquityAbstract', window );"><strong>Shareholders&#x2019; equity:</strong></a></td>
<td class="text">&#xA0;<span></span></td>
<td class="text">&#xA0;<span></span></td>
</tr>
<tr class="ro">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_CommonStockValue', window );">Common stock, $0 par value, 30,000,000 shares authorized, 13,312,302 and 9,312,302 shares issued and outstanding at December 31, 2012 and 2011, respectively</a></td>
<td class="nump">1,542,651<span></span></td>
<td class="nump">946,151<span></span></td>
</tr>
<tr class="re">
<td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AccumulatedDepreciationDepletionAndAmortizationPropertyPlantAndEquipment', window );">Accumulated deficit</a></td>
<td class="num">(1,145,758)<span></span></td>
<td class="num">(837,310)<span></span></td>
</tr>

**Many many HTML code here...... Many Many Many**
<td><strong> Name:</strong></td>
<td><nobr>us-gaap_StockholdersEquity</nobr></td>
</tr>
<tr>
<td style="padding-right: 4px;"><nobr><strong> Namespace Prefix:</strong></nobr></td>
<td>us-gaap_</td>
</tr>
<tr>
<td><strong> Data Type:</strong></td>
<td>xbrli:monetaryItemType</td>
</tr>
<tr>
<td><strong> Balance Type:</strong></td>
<td>credit</td>
</tr>
<tr>
<td><strong> Period Type:</strong></td>
<td>instant</td>
</tr>
</table>
</div>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>
</TEXT>
</DOCUMENT>

代码是这样的,但是要长一些

最佳答案

由于不清楚您要完成的确切目标,下面的解决方案将 HTML 文档分解为单独的标签和文本行。可能有一些特殊情况未处理,但它确实处理属性字符串,以防它们包含结束标记定界符。它写得很快,没有做太多测试,所以我会把任何必要的修复留给你。它不是很漂亮,但可以使用,应该足以让您入门。

#include <vector>
#include <string>
#include <iostream>


int main()
{
std::string html("<div style=\"width: 200px;\"><strong>Balance Sheets (USD $)<br></strong></div>");
std::vector<std::string> tags;
std::vector<std::string> text;

for(;;)
{
std::string::size_type startpos;

startpos = html.find('<');
if(startpos == std::string::npos)
{
// no tags left only text!
text.push_back(html);
break;
}

// handle the text before the tag
if(0 != startpos)
{
text.push_back(html.substr(0, startpos));
html = html.substr(startpos, html.size() - startpos);
startpos = 0;
}

// skip all the text in the html tag
std::string::size_type endpos;
for(endpos = startpos;
endpos < html.size() && html[endpos] != '>';
++endpos)
{
// since '>' can appear inside of an attribute string we need
// to make sure we process it properly.
if(html[endpos] == '"')
{
endpos++;
while(endpos < html.size() && html[endpos] != '"')
{
endpos++;
}
}
}

// Handle text and end of html that has beginning of tag but not the end
if(endpos == html.size())
{
html = html.substr(endpos, html.size() - endpos);
break;
}
else
{
// handle the entire tag
endpos++;
tags.push_back(html.substr(startpos, endpos - startpos));
html = html.substr(endpos, html.size() - endpos);
}
}

std::cout << "tags:\n-----------------" << std::endl;

// auto, iterators or range based for loop would probably be better but
// this makes it a bit easier to read.
for(size_t i = 0; i < tags.size(); i++)
{
std::cout << tags[i] << std::endl;
}

std::cout << "\ntext:\n-----------------" << std::endl;
for(size_t i = 0; i < text.size(); i++)
{
std::cout << text[i] << std::endl;
}
}

上面的代码生成以下输出(在 < 之后没有空格,因为 SO markdown 将它解释为一个 HTML 标签,就像它应该的那样)

tags:

< div style="width: 200px;">
< strong>
< br>
< /strong>
< /div>

text:

Balance Sheets (USD $)

关于c++ - 匹配字符串长度;查找和替换;在 C++ 中删除 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16922517/

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