【问题标题】:Modifying existing regular expression to retrieve the index修改现有正则表达式以检索索引
【发布时间】:2015-02-08 17:04:01
【问题描述】:

我有一个 html,但索引很少。示例如下

this is first sample index <!-- @@struct1_s§var1-->19.5.1<!--Index--> and this is required

this is second sample index <!-- @@struct2_s§var2-->19.5.2<!--Index--> - this is extension to the sample index <!-- @@struct3_s§var3-->19.5.3<!--Index--> and this is required.

我使用正则表达式

"<!--\s?@{2}[\.\w]*§[\.\w\[.\]]+-->[\d]+\.[\d]+\.[\d]+<!--Index-->"

所以如果我解析上面的 html 部分,我会得到匹配

<!-- @@struct1_s§var1-->19.5.1<!--Index-->
<!-- @@struct2_s§var2-->19.5.2<!--Index-->
<!-- @@struct3_s§var3-->19.5.3<!--Index-->

现在我想改变我的正则表达式,如果存在的话,我想检索整个索引

因此,如果我解析了 html 的上述部分,我应该得到匹配为

<!-- @@struct1_s§var1-->19.5.1<!--Index-->
<!-- @@struct2_s§var2-->19.5.2<!--Index--> - <!-- @@struct3_s§var3-->19.5.3<!--Index-->

我正在尝试使用此表达式,但它也会检索同一行中存在的其他字符

regEx.Pattern = "<!--\s?@{2}[\.\w]*§[\.\w\[.\]]+-->[\d]+\.[\d]+\.[\d]+<!--Index-->[-\s?]?[<!--\s?@{2}[\.\w]*§[\.\w\[.\]]+-->[\d]+\.[\d]+\.[\d]+<!--Index-->]?"

如果有人可以帮我解决这个问题。

【问题讨论】:

  • &lt;!--.*&lt;!--Index--&gt;
  • vks 能不能给我完整的表达方式
  • 它没有出现。也许你需要把它们放在花括号之间

标签: html regex excel vba


【解决方案1】:

更简单的版本可以在三场比赛中完成;它可以满足您对上述测试用例的要求:

<!--\s?@{2}[\.\w]*§[\.\w\[.\]]+-->\d+\.\d+\.\d+<!--Index-->( - )?

你可以在两次匹配中做你想做的,但你必须匹配比你想要的更多,然后使用第一个和第二个捕获组来获取你想要的匹配部分(通常是 $1 和 $2)。

(<!--\s?@{2}[\.\w]*§[\.\w\[.\]]+-->\d+\.\d+\.\d+<!--Index-->(?: - )?)(?:.*(<!--\s?@{2}[\.\w]*§[\.\w\[.\]]+-->\d+\.\d+\.\d+<!--Index-->))?

如果你能做到的话,第一个更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-24
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 2018-09-06
    • 2018-03-02
    • 1970-01-01
    相关资源
    最近更新 更多