diff --git a/src/org/openstreetmap/josm/tools/PatternUtils.java b/src/org/openstreetmap/josm/tools/PatternUtils.java
new file mode 100644
index 0000000000..b1e41a7873
--- /dev/null
+++ b/src/org/openstreetmap/josm/tools/PatternUtils.java
@@ -0,0 +1,57 @@
+// License: GPL. For details, see LICENSE file.
+package org.openstreetmap.josm.tools;
+
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
+
+import org.apache.commons.jcs3.access.CacheAccess;
+import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
+import org.openstreetmap.josm.data.cache.JCSCacheManager;
+import org.openstreetmap.josm.spi.preferences.Config;
+
+/**
+ * A class that caches compiled patterns.
+ * @author Taylor Smock
+ * @since xxx
+ */
+public final class PatternUtils {
+    /** A string that is highly unlikely to appear in regexes to split a regex from its flags */
+    private static final String MAGIC_STRING = "========";
+    /** A cache for Java Patterns (no flags) */
+    private static final CacheAccess<String, Pattern> cache = JCSCacheManager.getCache("java:pattern",
+            Config.getPref().getInt("java.pattern.cache", 1024), 0, null);
+
+    static {
+        // We don't want to keep these around forever, so set a reasonablish max idle life.
+        final IElementAttributes defaultAttributes = cache.getDefaultElementAttributes();
+        defaultAttributes.setIdleTime(TimeUnit.HOURS.toSeconds(1));
+        cache.setDefaultElementAttributes(defaultAttributes);
+    }
+
+    private PatternUtils() {
+        // Hide the constructor
+    }
+
+    /**
+     * Compile a regex into a pattern. This may return a {@link Pattern} used elsewhere. This is safe.
+     * @param regex The regex to compile
+     * @return The immutable {@link Pattern}.
+     * @see Pattern#compile(String)
+     */
+    public static Pattern compile(final String regex) {
+        return compile(regex, 0);
+    }
+
+    /**
+     * Compile a regex into a pattern. This may return a {@link Pattern} used elsewhere. This is safe.
+     * @param regex The regex to compile
+     * @param flags The flags from {@link Pattern} to apply
+     * @return The immutable {@link Pattern}.
+     * @see Pattern#compile(String, int)
+     */
+    public static Pattern compile(String regex, int flags) {
+        // Right now, the maximum value of flags is 511 (3 characters). This should avoid unnecessary array copying.
+        final StringBuilder sb = new StringBuilder(3 + MAGIC_STRING.length() + regex.length());
+        return cache.get(sb.append(flags).append(MAGIC_STRING).append(regex).toString(), () -> Pattern.compile(regex, flags));
+    }
+}
diff --git a/src/org/openstreetmap/josm/tools/Tag2Link.java b/src/org/openstreetmap/josm/tools/Tag2Link.java
index e4c91b1fbf..136b5d96b6 100644
--- a/src/org/openstreetmap/josm/tools/Tag2Link.java
+++ b/src/org/openstreetmap/josm/tools/Tag2Link.java
@@ -10,11 +10,12 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.EnumMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.Set;
 import java.util.function.Supplier;
 import java.util.function.UnaryOperator;
 import java.util.regex.Matcher;
@@ -147,7 +148,7 @@ public final class Tag2Link {
             return;
         }
 
-        final HashMap<OsmPrimitiveType, Optional<ImageResource>> memoize = new HashMap<>();
+        final Map<OsmPrimitiveType, Optional<ImageResource>> memoize = new EnumMap<>(OsmPrimitiveType.class);
         final Supplier<ImageResource> imageResource = () -> memoize
                 .computeIfAbsent(OsmPrimitiveType.NODE, type -> OsmPrimitiveImageProvider.getResource(key, value, type))
                 .orElse(null);
@@ -208,11 +209,23 @@ public final class Tag2Link {
                     tr("View category on Wikimedia Commons"), getWikimediaCommonsUrl(i), imageResource.get()));
         }
 
-        wikidataRules.getValues(key).forEach(urlFormatter -> {
+        final Set<String> formatterUrls = wikidataRules.getValues(key);
+        if (!formatterUrls.isEmpty()) {
             final String formattedValue = valueFormatter.getOrDefault(key, x -> x).apply(value);
-            final String url = urlFormatter.replace("$1", formattedValue);
-            linkConsumer.acceptLink(getLinkName(url, key), url, imageResource.get());
-        });
+            final Pattern dollarOne = PatternUtils.compile("$1", Pattern.LITERAL);
+
+            final String urlKey = formatterUrls.stream().map(urlFormatter -> dollarOne.matcher(urlFormatter)
+                            .replaceAll(Matcher.quoteReplacement("(.*)"))).map(PatternUtils::compile)
+                            .map(pattern -> pattern.matcher(value)).filter(Matcher::matches)
+                            .map(matcher -> matcher.group(1)).findFirst().orElse(formattedValue);
+
+            formatterUrls.forEach(urlFormatter -> {
+                // Check if the current value matches the formatter pattern -- some keys can take a full url or a key for
+                // the formatter. Example: https://wiki.openstreetmap.org/wiki/Key:contact:facebook
+                final String url = urlFormatter.replace("$1", urlKey);
+                linkConsumer.acceptLink(getLinkName(url, key), url, imageResource.get());
+            });
+        }
     }
 
     private static String getWikimediaCommonsUrl(String i) {
