匹配字符串长度;查找和替换;删除C ++中的HTML标记

时间:2013-06-04 16:05:15

标签: c++ html string visual-studio-2010

这是我的问题 我正在尝试删除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>

代码就像这样,但更长的时间

3 个答案:

答案 0 :(得分:3)

由于目前尚不清楚您要完成的是什么,因此下面的解决方案会将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;
    }
}

上面的代码生成以下输出(在&lt;之后没有空格,因为SO markdown将它解释为一个类似它的HTML标记)

  

标签:

     

&LT; div style =“width:200px;”&gt;
  &LT;强&GT;
  &LT; BR&GT;
  &LT; /强&GT;
  &LT; / DIV&GT;

     

文本:

     

资产负债表(美元)

答案 1 :(得分:1)

正则表达式也不是HTML的一种方式。尝试使用一些HTML解析库。 示例包括ExpatXerces

这些库可帮助您阅读整个HTML或html标签及其属性等的文本表示。

答案 2 :(得分:0)

一个建议是获取html中的所有标签并将它们添加到哈希映射中 然后读取一个标记(在“&lt;”之后)并使用哈希映射进行搜索,并使您的代码相应地起作用。