【问题标题】:Strings and Pointer locations字符串和指针位置
【发布时间】:2012-09-14 02:20:52
【问题描述】:

我正在开发一个程序,该程序需要在 HTML/XML 垃圾遍地的 .txt 文件中筛选出一个末尾有数字的特定模式。这种模式应该出现 10 次。模式如下:"<p class="wx-temp"> 93." 93 是温度读数,我最终要收集的数据,但是,我找不到将 93 与字符串的其余部分隔离的方法,因为它会随着该程序将理想地运行的每一天。我一直在尝试找到一种方法来定义不能为常量的整数数据类型(即,我不能在字符串末尾输入 93,因为它会破坏目的)并将其放在字符串或其他东西中类似于我可以在模式结束后设置 X 个字符开始,或者换句话说,指针位置。很抱歉漫无边际。有人可以帮帮我吗?

【问题讨论】:

  • 我昨天不是回复a very similar question of yours了吗?您应该显示包含您的号码的标记的 sn-p,以便给出合理的答案。
  • @paddy:你必须使用反引号,否则尖括号不会显示。
  • 您可以尝试使用regular expressions搜索。

标签: c++ string parsing visual-c++


【解决方案1】:

假设您已经将整个文件加载到一个字符串中,这不是不合理的。

string html;
//(Some code that reads into a big string)

现在你只需要寻找那个标签。

string delimiter( "<p class=\"wx-temp\">" );
vector<int> temperatures;

size_t pos = html.find_first_of(delimiter);
while( pos != string::npos ) 
{
    // Skip past the tag (to the temperature)
    pos += delimiter.size();
    if( pos >= html.size() ) break;

    // Extract it (C-style) and chuck it into the vector.
    int temperature = atoi( html.c_str() + pos );
    temperatures.push_back(temperature);

    // If you want to stop after the first 10:
    if( temperatures.size() == 10 ) break; 

    // Find the next tag
    pos = html.find_first_of(delimiter, pos);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2015-02-22
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多