LearnDevOpsHands-On Labs
Back to All DevOps Tools
java.util.regex Studio

Java Regex Tester

Test regular expressions for Java's Pattern and Matcher classes. Automatically doubles backslashes for Java string literals and previews matcher.replaceAll().

""

Matcher Results (2)matcher.find()

10.0.1.5:8080[27, 40]
matcher.group(name):
ip:"10.0.1.5"
port:"8080"
10.0.1.6:9090[57, 70]
matcher.group(name):
ip:"10.0.1.6"
port:"9090"

matcher.replaceAll() Preview

${name}
Cluster nodes: worker-1 at Endpoint: 10.0.1.5 (Port 8080) and worker-2 at Endpoint: 10.0.1.6 (Port 9090).

Java Code Generator

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexExample {
    public static void main(String[] args) {
        String input = "Cluster nodes: worker-1 at 10.0.1.5:8080 and worker-2 at 10.0.1.6:9090.";
        Pattern pattern = Pattern.compile("(?<ip>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(?<port>\\d+)");
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            System.out.println("Match: " + matcher.group());
            System.out.println("Span: [" + matcher.start() + ", " + matcher.end() + "]");
        }

        String replaced = matcher.replaceAll("Endpoint: ${ip} (Port ${port})");
        System.out.println("Result: " + replaced);
    }
}