正则表达式获取两个单词之间的文本
Regex Get Text Between 2 Words
我需要输入一个词并获取包含词的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.*?<\/>|<.*?\/>
请尝试以下操作:
*编辑 - 现在变得有点混乱(而且相当“hacky”)...
/<([pali]{1,2})>[^<>]*Madhuparna[^<>]*<\/>/g
可能没有完全优化,但按照您的示例完成了工作。
这假定您遇到的唯一标签(根据您的示例)是 <p>
和 <a>
,但如果需要请更新正则表达式的第一个捕获组 ([pa])
。
此处证明:https://regex101.com/r/16jjLn/1 -(已更新)
上面 link 上的解释面板将解释正则表达式的作用。
我刚刚测试过这个。它运作良好。
请再试一次。
/<(\S+)(>| .*?>)[^<>]*Madhuparna[^<>]*<\/>/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>
这可以应用于所有标签。
我不确定这是否适合您,但希望对您有所帮助。 :)
我需要输入一个词并获取包含词的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.*?<\/>|<.*?\/>
请尝试以下操作:
*编辑 - 现在变得有点混乱(而且相当“hacky”)...
/<([pali]{1,2})>[^<>]*Madhuparna[^<>]*<\/>/g
可能没有完全优化,但按照您的示例完成了工作。
这假定您遇到的唯一标签(根据您的示例)是 <p>
和 <a>
,但如果需要请更新正则表达式的第一个捕获组 ([pa])
。
此处证明:https://regex101.com/r/16jjLn/1 -(已更新)
上面 link 上的解释面板将解释正则表达式的作用。
我刚刚测试过这个。它运作良好。 请再试一次。
/<(\S+)(>| .*?>)[^<>]*Madhuparna[^<>]*<\/>/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>
这可以应用于所有标签。
我不确定这是否适合您,但希望对您有所帮助。 :)