我想尝试匹配强标签之间字符串的内部部分,以保证强标签内的强以Price Range:
开头。 。此文本不应出现在字符串中的其他任何位置,但 <p>
除外。和<strong>
标签当然可以。我怎样才能将它与groovy相匹配?
<p><strong>Price Range: $61,000-$99,500</strong></p>
我尝试过:
def string = "<p><strong>Price Range: \$61,000-\$181,500</strong></p>strong";
string = string.replace(/Price.*strong/, "Replaced");
只是想看看我是否可以得到一些东西,但我似乎无法得到任何超过一个单词的东西,这当然不是特别有用,因为我不需要正则表达式。
最佳答案
发现问题。
def string = "<p><strong>Price Range: \$61,000-\$181,500</strong>?</p>strong";
string = string.replaceFirst(~/<strong>Price Range.*<\/strong>/, "Replaced");
这包括强标签,但对于我的目的来说已经足够了。开头需要 replaceFirst
而不是 replace
和 ~
来指示正则表达式。
关于java - Groovy 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5280130/