Muhd
Muhd

Reputation: 25566

Groovy Regular Expressions

I wanted to try to match the inner part of the string between the strong tags where it is guaranteed that the strong inside the strong tags starts with Price Range:. This text should not appear anywhere else in the string, but the <p> and <strong> tags certainly do. How can I match this with groovy?

<p><strong>Price Range: $61,000-$99,500</strong></p>

I tried:

def string = "<p><strong>Price Range: \$61,000-\$181,500</strong></p>strong";

string = string.replace(/Price.*strong/, "Replaced");

Just to see if I could get something to work, but I can't seem to get anything working that is more than a single word, which of course isn't particularly useful since I don't need regex for that.

Upvotes: 1

Views: 317

Answers (2)

Pablo Fernandez
Pablo Fernandez

Reputation: 105220

Is this what you're trying to do?

http://regexr.com?2t9jp

Upvotes: 0

Muhd
Muhd

Reputation: 25566

Found the problems.

def string = "<p><strong>Price Range: \$61,000-\$181,500</strong>?</p>strong";

string = string.replaceFirst(~/<strong>Price Range.*<\/strong>/, "Replaced");

This includes the strong tags but it is good enough for my purpose. Needed replaceFirst instead of replace and ~ at the start to indicate a regex.

Upvotes: 1

Related Questions