How do I split up string using regular expression?

This code snippet uses the java.util.regex.Pattern.split() method to split-up input string separated by commas or whitespaces (spaces, tabs, new lines, carriage returns, form feeds).

package org.kodejava.regex;

import java.util.regex.Pattern;

public class RegexSplitExample {
    public static void main(String[] args) {
        // Pattern for finding commas, whitespaces (spaces, tabs, new lines,
        // carriage returns, form feeds).
        String pattern = "[,\\s]+";
        String colors = """
                Red,White, Blue   Green        Yellow,
                Orange Pink""";

        Pattern splitter = Pattern.compile(pattern);
        String[] results = splitter.split(colors);

        for (String color : results) {
            System.out.format("Color = \"%s\"%n", color);
        }
    }
}

The result of our code snippet is:

Color = "Red"
Color = "White"
Color = "Blue"
Color = "Green"
Color = "Yellow"
Color = "Orange"
Color = "Pink"

How do I create a string search and replace using regex?

In this example you’ll see how we can create a small search and replace program using the regular expression classes in Java. The code below will replace all the brown words to red.

package org.kodejava.regex;

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

public class StringReplace {
    public static void main(String[] args) {
        String source = "The quick brown fox jumps over the brown lazy dog.";
        String find = "brown";
        String replace = "red";

        // Compiles the given regular expression into a pattern.
        Pattern pattern = Pattern.compile(find);

        // Creates a matcher that will match the given input against the
        // pattern.
        Matcher matcher = pattern.matcher(source);

        // Replaces every subsequence of the input sequence that matches
        // the pattern with the given replacement string.
        String output = matcher.replaceAll(replace);

        System.out.println("Source = " + source);
        System.out.println("Output = " + output);
    }
}

The result of the code snippet is:

Source = The quick brown fox jumps over the brown lazy dog.
Output = The quick red fox jumps over the red lazy dog.

How do I do regular expression in Java?

This example demonstrates how we do regular expression in Java. The regular expression classes is in the java.uti.regex package. The main class including the java.util.regex.Pattern class and the java.util.regex.Matcher class.

In this example we are only testing to match a string literal if it is exists in the following sentence, we are searching the word “lazy”.

package org.kodejava.regex;

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

public class RegexDemo {
    public static void main(String[] args) {
        /*
         * To create a Pattern instance we must call the static method
         * called compile() in the Pattern class. Pattern object is
         * the compiled representation of a regular expression.
         */
        Pattern pattern = Pattern.compile("lazy");

        /*
         * The Matcher class also doesn't have the public constructor
         * so to create a matcher call the Pattern's class matcher()
         * method. The Matcher object itself is the engine that match
         * the input string against the provided pattern.
         */
        String input = "The quick brown fox jumps over the lazy dog";
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            System.out.format("Text \"%s\" found at %d to %d.%n",
                    matcher.group(), matcher.start(), matcher.end());
        }
    }
}

The output of the code snippet above is:

Text "lazy" found at 35 to 39.