From cce9f3e7397d84b4b7de83781f17aa61e22a6a88 Mon Sep 17 00:00:00 2001
From: Daniil Zhyliaiev <yangroang@gmail.com>
Date: Mon, 13 Jan 2025 19:42:30 +0200
Subject: [PATCH] `LatestRelease` should replace `metadataPattern` as regex, as
 documented (#4894)

* Fix version comparator

* LatestRelease.metadataPattern should be matched as a regex

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
---
 .../src/main/java/org/openrewrite/semver/LatestRelease.java  | 5 ++---
 .../org/openrewrite/gradle/UpgradeDependencyVersionTest.java | 2 +-
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/rewrite-core/src/main/java/org/openrewrite/semver/LatestRelease.java b/rewrite-core/src/main/java/org/openrewrite/semver/LatestRelease.java
index df8fac8951c..0b1fa2b0095 100644
--- a/rewrite-core/src/main/java/org/openrewrite/semver/LatestRelease.java
+++ b/rewrite-core/src/main/java/org/openrewrite/semver/LatestRelease.java
@@ -140,9 +140,8 @@ public int compare(@Nullable String currentVersion, String v1, String v2) {
         // parts are the same:
         //
         // HyphenRange [25-28] should include "28-jre" and "28-android" as possible candidates.
-        String normalized1 = metadataPattern == null ? nv1 : nv1.replace(metadataPattern, "");
-        String normalized2 = metadataPattern == null ? nv2 : nv1.replace(metadataPattern, "");
-
+        String normalized1 = metadataPattern == null ? nv1 : nv1.replaceAll(metadataPattern, "");
+        String normalized2 = metadataPattern == null ? nv2 : nv2.replaceAll(metadataPattern, "");
         try {
             for (int i = 1; i <= Math.max(vp1, vp2); i++) {
                 String v1Part = v1Gav.group(i);
diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java
index e8d6d145923..2626825c559 100644
--- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java
+++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java
@@ -1016,7 +1016,7 @@ void exactVersionWithExactPattern() {
     @Issue("https://github.com/openrewrite/rewrite/issues/4333")
     void exactVersionWithRegexPattern() {
         rewriteRun(
-          spec -> spec.recipe(new UpgradeDependencyVersion("com.google.guava", "guava", "32.1.1", ".*droid")),
+          spec -> spec.recipe(new UpgradeDependencyVersion("com.google.guava", "guava", "32.1.1", "-.*?droid")),
           buildGradle(
             """
               plugins {