【问题标题】:Regex Get Text Between 2 Words正则表达式获取两个单词之间的文本
【发布时间】:2021-06-25 23:08:32
【问题描述】:

我需要放一个词,获取包含这个词的html标签。 示例:

  • 文本输入:Madhuparna

我需要获取:

  • 2021 年 6 月 5 日,作者 Madhuparna

  • bla bla bla Madhuparna bla bla bla

测试文本:

<p>The entire purpose speed up the process.</p><p>June 5, 2021 By Madhuparna</p>\r\n<p>The entire purpose of a terminal emulator is to imitate how the regular computer terminals perform and allowing the main computer to connect to and use a remote computer through a command-line or a graphical interface. The terminal emulators are known to carry out the functions using the software.</p>\r\n<a>It allows file transfer between the main and the remote computer using SSH (Secure Shell) and also enables the host system to execute applications on the remote system. While it features a graphical user interface, programmers rather prefer the text-based interface to gain more control over all functions and speed up the process.</a><p>bla bla bla Madhuparna bla bla bla</p>

我现在做什么但不工作:

<(\S*?)[^>]*>.*?Madhuparna.*?<\/\1>|<.*?\/>

【问题讨论】:

    标签: regex uipath


    【解决方案1】:

    请尝试以下方法:

    *edit - 现在变得有点混乱(而且相当“hacky”)......

    /<([pali]{1,2})>[^<>]*Madhuparna[^<>]*<\/\1>/g
    

    可能没有完全优化,但可以根据您的示例完成工作。

    这假设您遇到的唯一标签(根据您的示例)是 &lt;p&gt;&lt;a&gt;,但如果需要,请更新正则表达式的第一个捕获组 ([pa])

    这里的证明:https://regex101.com/r/16jjLn/1 -(更新)

    上面链接上的解释面板将解释正则表达式的作用。

    【讨论】:

    • 如果我有
    • ?我需要修改什么?我放了 [pal] 但不工作
  • 我创建了这个并且看起来不错:[^]*Madhuparna[^]*
  • 如果需要,我该怎么做:在文本中找到最后一个单词“Madhuparna”,然后从左边第一个 [pali]{1,2} 到最后一个 或 获取文本li> 在文中。文本输入示例:
  • bla bla bla
  • 2021 年 6 月 5 日,作者 Madhuparna

  • 使用远程计算机
  • 我需要的结果:

    2021 年 6 月 5 日作者 Madhuparna

  • 使用远程计算机
  • 【解决方案2】:

    我刚刚对此进行了测试。它运作良好。 请再试一次。

    /<(\S+)(>| .*?>)[^<>]*Madhuparna[^<>]*<\/\1>/g
    

    我刚刚像这样更改了您的测试文本。

    <p>The entire purpose speed up the process.</p>
    <p class="test">June 5, 2021 By Madhuparna</p>
    <p >The entire purpose of a terminal emulator is to imitate how the regular computer terminals perform and allowing the main computer to connect to and use a remote computer through a command-line or a graphical interface. The terminal emulators are known to carry out the functions using the software.</p>
    <a>It allows file transfer between the main and the remote computer using SSH (Secure Shell) and also enables the host system to execute applications on the remote system. While it features a graphical user interface, programmers rather prefer the text-based interface to gain more control over all functions and speed up the process.</a>
    <span>bla bla bla Madhuparna bla bla bla</span>
    

    这可以应用于所有标签。

    我不确定这是否适合您,但希望对您有所帮助。 :)

    【讨论】:

    • 不起作用,因为选择所有文本,只有我需要从标签

      并包含所选单词:Madhuparna
    • 您可以在上面再试一次。如果您在标签中使用 attr,那么也可以正常工作。如果你只想要

      标签,你可以像这样使用。 /| .*?>)[^]*Madhuparna[^]*/g

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签