QRegExp 在 html 中的标签之间提取字符串

QRegExp to extract string between a tag in html

情况很棘手,因为我无法访问 qt 模块上的 webkit,我被迫使用 QRegExp 解析 HTML 文件:

该文件包含我需要提取的字符串,这些字符串位于 li 标签之间。

如果我写一个QRegExp

QRegExp ("[^</li>]([a-zA-Z0-9_./]+)");

我可以提取 li 标签之间的所有字符串。但我只需要:

Pg_1_qds_Bin_Indicator_2

Pg_1_qds_Bin_Indicator_3

Pg_1_qds_Ana_Indicator_1 和li

之间的所有类似的名字

有些其他名称不在所附文件的一部分,但在完整文件中: TEMPLATE_LOGO

Pg_1_Command_By_Text

所有名称都以 Pg_ 开头,除了一个 TEMPLATE_LOGO_

我觉得其他行有 [ 之类的字符,或者中间有另一个标记来标识该行不需要该字符串。

文件位于下方, 所以,长话短说;博士 需要一个 QRegExp 来提取在 li 标签之间找到的上述名称。

<ul>
  <li><a href="#symbols">Symbol report</a></li>
<ul>
  <li><a href="#symbolsConsistency">Consistency</a></li>
  <li><a href="#symbolCharacteristics">Symbol characteristics</a></li>
  <li><a href="#basicSymbols">Display of basic symbols</a></li>


    <ul>
      <li>Pg_1_qds_Bin_Indicator_2</li>
    <ul>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionalignment] = (Right)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptiontextcolor] = (Color {0, 0, 0, 255})</li>
      <li>[QDSConsistency.report.field.logicIndicator.isdescriptiondisplayed] = (true)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionfontfamily] = (Arial)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionfontsize] = (11 pt)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionfontstyle] = (Normal)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionfontposition] = (LEFT)</li>
      <li>[QDSConsistency.report.field.analogIndicator.descriptiontext] = (v1)</li>
      <li>[QDSConsistency.report.field.logicIndicator.backgroundcolor] = (Color {238, 238, 238, 255})</li>
      <li>[QDSConsistency.report.field.logicIndicator.digitnumber] = (8)</li>
      <li>[QDSConsistency.report.field.logicIndicator.widgetfont] = (FONT1)</li>
      <li>[QDSConsistency.report.field.logicIndicator.shortname] = (Pg_1_qds_Bin_Indicator_2)</li>
      <li>[QDSConsistency.report.field.logicIndicator.precision] = (2)</li>
      <li>[QDSConsistency.report.field.logicIndicator.widgetuserfontfamily] = (Arial)</li>
      <li>[QDSConsistency.report.field.logicIndicator.widgetuserfontsize] = (11 pt)</li>
      <li>[QDSConsistency.report.field.analogIndicator.widgetuserfontstyle] = (NORMAL)</li>
      <li>[QDSConsistency.report.field.analogIndicator.dynamicbackgroundcolor] = (Color {238, 238, 238, 255})</li>
      <li>[QDSConsistency.report.field.analogIndicator.longname] = (Pg_1_qds_Bin_Indicator_2_v1)</li>
      <li>[QDSConsistency.report.field.logicIndicator.heith] = (32)</li>
      <li>[QDSConsistency.report.field.logicIndicator.weigth] = (50)</li>
      <li>[QDSConsistency.report.field.logicIndicator.poxX] = (352)</li>
      <li>[QDSConsistency.report.field.logicIndicator.poxY] = (116)</li>
      <li>[QDSConsistency.report.field.logicIndicator.valuealignment] = (Left)</li>
      <li>[QDSConsistency.report.field.logicIndicator.value_0] = (Off)</li>
      <li>[QDSConsistency.report.field.logicIndicator.value_1] = (On)</li>
    </ul>
      <li>Pg_1_qds_Bin_Indicator_3</li>
    <ul>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionalignment] = (Right)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptiontextcolor] = (Color {0, 0, 0, 255})</li>
      <li>[QDSConsistency.report.field.logicIndicator.isdescriptiondisplayed] = (true)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionfontfamily] = (Arial)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionfontsize] = (11 pt)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionfontstyle] = (Normal)</li>
      <li>[QDSConsistency.report.field.logicIndicator.descriptionfontposition] = (LEFT)</li>
      <li>[QDSConsistency.report.field.analogIndicator.descriptiontext] = (v1)</li>
      <li>[QDSConsistency.report.field.logicIndicator.backgroundcolor] = (Color {238, 238, 238, 255})</li>
      <li>[QDSConsistency.report.field.logicIndicator.digitnumber] = (8)</li>
      <li>[QDSConsistency.report.field.logicIndicator.widgetfont] = (FONT1)</li>
      <li>[QDSConsistency.report.field.logicIndicator.shortname] = (Pg_1_qds_Bin_Indicator_3)</li>
      <li>[QDSConsistency.report.field.logicIndicator.precision] = (2)</li>
      <li>[QDSConsistency.report.field.logicIndicator.widgetuserfontfamily] = (Arial)</li>
      <li>[QDSConsistency.report.field.logicIndicator.widgetuserfontsize] = (11 pt)</li>
      <li>[QDSConsistency.report.field.analogIndicator.widgetuserfontstyle] = (NORMAL)</li>
      <li>[QDSConsistency.report.field.analogIndicator.dynamicbackgroundcolor] = (Color {238, 238, 238, 255})</li>
      <li>[QDSConsistency.report.field.analogIndicator.longname] = (Pg_1_qds_Bin_Indicator_3_v1)</li>
      <li>[QDSConsistency.report.field.logicIndicator.heith] = (32)</li>
      <li>[QDSConsistency.report.field.logicIndicator.weigth] = (50)</li>
      <li>[QDSConsistency.report.field.logicIndicator.poxX] = (446)</li>
      <li>[QDSConsistency.report.field.logicIndicator.poxY] = (187)</li>
      <li>[QDSConsistency.report.field.logicIndicator.valuealignment] = (Left)</li>
      <li>[QDSConsistency.report.field.logicIndicator.value_0] = (Off)</li>
      <li>[QDSConsistency.report.field.logicIndicator.value_1] = (On)</li>
    </ul>
    </ul>
    <p><em>Analog indicator :</em></p>
    <ul>
      <li>Pg_1_qds_Ana_Indicator_1</li>
    <ul>
      <li>[QDSConsistency.report.field.analogIndicator.descriptionalignment] = (Right)</li>
      <li>[QDSConsistency.report.field.analogIndicator.descriptiontextcolor] = (Color {0, 0, 0, 255})</li>
      <li>[QDSConsistency.report.field.analogIndicator.isdescriptiondisplayed] = (true)</li>
      <li>[QDSConsistency.report.field.analogIndicator.descriptionfontfamily] = (Arial)</li>
      <li>[QDSConsistency.report.field.analogIndicator.descriptionfontsize] = (11 pt)</li>
      <li>[QDSConsistency.report.field.analogIndicator.descriptionfontstyle] = (Normal)</li>
      <li>[QDSConsistency.report.field.analogIndicator.descriptionfontposition] = (LEFT)</li>
      <li>[QDSConsistency.report.field.analogIndicator.descriptiontext] = (v0)</li>
      <li>[QDSConsistency.report.field.analogIndicator.backgroundcolor] = (Color {238, 238, 238, 255})</li>
      <li>[QDSConsistency.report.field.analogIndicator.digitnumber] = (8)</li>
      <li>[QDSConsistency.report.field.analogIndicator.widgetfont] = (FONT1)</li>
      <li>[QDSConsistency.report.field.analogIndicator.shortname] = (Pg_1_qds_Ana_Indicator_1)</li>
      <li>[QDSConsistency.report.field.analogIndicator.precision] = (2)</li>
      <li>[QDSConsistency.report.field.analogIndicator.widgetuserfontfamily] = (Arial)</li>
      <li>[QDSConsistency.report.field.analogIndicator.widgetuserfontsize] = (11 pt)</li>
      <li>[QDSConsistency.report.field.analogIndicator.widgetuserfontstyle] = (NORMAL)</li>
      <li>[QDSConsistency.report.field.analogIndicator.dynamicbackgroundcolor] = (Color {238, 238, 238, 255})</li>
      <li>[QDSConsistency.report.field.analogIndicator.longname] = (Pg_1_qds_Ana_Indicator_1_v0)</li>
      <li>[QDSConsistency.report.field.analogIndicator.heith] = (32)</li>

根据您的评论,看看这是否适合您:<li>.*\((Pg_1[^)]*|TEMPLATE_LOGO).*?<\/li>

应匹配在 li 标签之间找到的任何以 "Pg_1" 或特别是 "TEMPLATE_LOGO" 开头的字符串。

不,您不会被迫使用 QRegExp 解析 html。 regular 表达式匹配器只能匹配 regular 语法语言。 HTML 不是一种具有常规语法的语言。所以it will not ever reliably work. Use an HTML parser! I suggest Gumbo。它是一个独立的基于 C 的解析器,具有易于使用的 API.

regex101.com/r/yG9aZ8/2 ure solution led to the final soultion and if u could post this I would close this post but i need to improve it for TEMPLATE_LOGO

然后只需添加 第二个备选方案:TEMPLATE_LOGO.*:

QRegExp exp1("<li>(Pg_.*|TEMPLATE_LOGO_.*)<\/li>");

感谢 Trey 先生和 Wiktor Stribiżew 先生,他们的回答导致了所需的解决方案。

感谢 Trey 先生和 Wiktor Stribiżew 先生,他们的回答导致了所需的解决方案。

QRegExp exp1("<li>(Pg_.*|TEMPLATE_LOGO_.*)<\/li>");