首页 > 解决方案 > 需要正则表达式来匹配两个单词,因为它们之间可能有任意数量的空格或其他字符

问题描述

假设我想匹配短语“Johnny Appleseed”,我们得到以下字符串:

<p>Johnny random blah stuff</p>
<p>more random stuff Appleseed blah blue bleh</p>

我希望能够匹配 Johnny 和 Appleseed 以便将这些单词包装在标签中,我该怎么做?

预期输出:

<p><span>Johnny</span> random blah stuff</p>
<p>more random stuff <span>Appleseed</span> blah blue bleh</p>

感谢所有建议!

注意:基本上我想要的是此页面自动使用示例字符串中的单词 Johnny 和 Appleseed 执行的操作(它使 Johhnny 和 Appleseed 的颜色与其他文本不同)

标签: javahtmlregex

解决方案


决定使用 Jsoup 来解析我的 HTML 而不是使用正则表达式。感谢时间!


推荐阅读