【问题标题】:Qt Regexp extract <p> tags from Html stringQt Regexp 从 Html 字符串中提取 <p> 标签
【发布时间】:2016-08-02 23:47:38
【问题描述】:

我有一个 RichText,我将 QTextEdit 中的 Html 源存储在一个字符串中。 我想做的是一一提取所有行(我有 4-6 行)。 字符串如下所示:

//html opening stuff
<p style = attributes...><span style = attributes...>My Text</span></p>
//more lines like this
//html closing stuff

所以我需要从开始 p 标签到结束 p 标签的整行(也包括 p 标签)。 我检查并尝试了在这里和其他网站上找到的所有内容,但仍然没有结果。

这是我的代码(“htmlStyle”是输入字符串):

QStringList list;
QRegExp rx("(<p[^>]*>.*?</p>)");
int pos = 0;

while ((pos = rx.indexIn(htmlStyle, pos)) != -1) {
    list << rx.cap(1);
    pos += rx.matchedLength();
}

或者有没有其他方法可以在没有正则表达式的情况下做到这一点?

【问题讨论】:

  • 这个问题是一个经典的 X-Y 问题,在您告诉我们为什么需要在富文本文档中迭代段落之前是不完整的。你将如何处理这些段落?请注意,“我将进一步解析它们”不是要走的路:您真的不想编写自己的 HTML 解析器。除非您对其进行解析,否则您无法处理其中包含一些 HTML 的文本字符串。将解析留给 Qt,无论如何它已经做到了。利用 Qt 允许您访问的 HTML 解析器。
  • 您可以尝试使用 QDomDocument 的 xml 阅读器

标签: html regex qt extract qregexp


【解决方案1】:

HTML/XML 不是常规语法。你不能用正则表达式解析它。参见例如this question。解析 HTML 并非易事。

您可以使用QTextDocumentQTextBlockQTextCursor 等对富文本文档中的段落进行迭代。所有的 HTML 解析都会为您处理。这正是QTextEdit 支持的HTML 子集:它使用QTextDocument 作为内部表示。您可以使用QTextEdit::document() 直接从小部件中获取它。例如:

void iterate(QTextEdit * edit) {
   auto const & doc = *edit->document();
   for (auto block = doc.begin(); block != doc.end(); block.next()) {
      // do something with text block e.g. iterate its fragments
      for (auto fragment = block.begin(); fragment != block.end(); fragment++) {
         // do something with text fragment
      }
   }
}

您应该探索QTextDocument 的结构并根据需要使用它,而不是手动错误地解析 HTML。

【讨论】:

    【解决方案2】:

    下面是纯java方式,希望对你有帮助:

    int startIndex = htmlStyle.indexOf("<p>");
            int endIndex = htmlStyle.indexOf("</p>");
            while (startIndex >= 0) {
                endIndex = endIndex + 4;// to include </p> in the substring
                System.out.println(htmlStyle.substring(startIndex, endIndex));
                startIndex = htmlStyle.indexOf("<p>", startIndex + 1);
                endIndex = htmlStyle.indexOf("</p>", endIndex + 1);
            }
    

    【讨论】:

    • 谢谢,我想它会起作用的!我只需要考虑一下。 Qt 中没有子字符串。我尝试使用“section()”,它返回 4 个空字符串,这部分是好的,因为我当前的字符串中有 4 个匹配项。我只需要弄清楚如何获取实际文本。
    • 现在它就像一个魅力!再次感谢! QStringRef subString(&amp;htmlStyle, startIndex, endIndex-startIndex); 从 startIndex 进入 htmlStyle 到 endIndex-startIndex 长度后停止!
    【解决方案3】:

    对于那些需要完整 Qt 解决方案的人,我根据@Aditya Poorna 的回答找到了答案。感谢您的提示!

    代码如下:

    int startIndex = htmlStyle.indexOf("<p");
    int endIndex = htmlStyle.indexOf("</p>");
    
    while (startIndex >= 0) {
        endIndex = endIndex + 4;
        QStringRef subString(&htmlStyle, startIndex, endIndex-startIndex);
        qDebug() << subString;
        startIndex = htmlStyle.indexOf("<p", startIndex + 1);
        endIndex = htmlStyle.indexOf("</p>", endIndex + 1);
    }
    

    “QStringRef subString”从“startIndex”进入“htmlStyle”直到“endIndex-startIndex”的长度!

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      相关资源
      最近更新 更多