Regular Expressions in Java

Our program demonstrates common regular expression tasks in Java. Here’s the full source code with explanations:

import java.util.regex.*;
import java.util.Arrays;

public class RegularExpressions {
    public static void main(String[] args) {
        // This tests whether a pattern matches a string.
        boolean match = Pattern.matches("p([a-z]+)ch", "peach");
        System.out.println(match);

        // For other regex tasks, you'll need to compile a Pattern object.
        Pattern r = Pattern.compile("p([a-z]+)ch");

        // Many methods are available on these objects. Here's a match test like we saw earlier.
        Matcher m = r.matcher("peach");
        System.out.println(m.matches());

        // This finds the match for the regex.
        m = r.matcher("peach punch");
        m.find();
        System.out.println(m.group());

        // This also finds the first match but returns the start and end indexes for the match.
        m.reset();
        m.find();
        System.out.println("idx: [" + m.start() + " " + m.end() + "]");

        // The group methods include information about both the whole-pattern matches 
        // and the submatches within those matches.
        m.reset();
        m.find();
        System.out.println(Arrays.toString(new String[]{m.group(), m.group(1)}));

        // Similarly, this will return information about the indexes of matches and submatches.
        m.reset();
        m.find();
        System.out.println(Arrays.toString(new int[]{m.start(), m.end(), m.start(1), m.end(1)}));

        // To find all matches in the input, we can use a while loop with find().
        m = r.matcher("peach punch pinch");
        while (m.find()) {
            System.out.print(m.group() + " ");
        }
        System.out.println();

        // The replaceAll method can be used to replace subsets of strings with other values.
        String result = r.matcher("a peach").replaceAll("<fruit>");
        System.out.println(result);

        // The replaceAll method can also use a function to transform matched text.
        result = r.matcher("a peach").replaceAll(mr -> mr.group().toUpperCase());
        System.out.println(result);
    }
}

To run the program, compile and execute it:

$ javac RegularExpressions.java
$ java RegularExpressions
true
true
peach
idx: [0 5]
[peach, ea]
[0, 5, 1, 3]
peach punch pinch 
a <fruit>
a PEACH

Java’s regular expression support is provided by the java.util.regex package. The Pattern class is used to compile regular expressions, and the Matcher class is used to perform match operations on a character sequence.

Unlike Go, Java doesn’t have separate methods for string and byte slice operations. All operations are performed on strings or character sequences.

The matches method of Pattern is equivalent to Go’s MatchString. The find method of Matcher is similar to Go’s FindString, but it updates the state of the Matcher object instead of returning a new string.

Java’s regular expression package doesn’t have direct equivalents for all of Go’s methods. For example, there’s no built-in method to find all matches with a limit. However, you can implement this functionality using a counter with the find method.

For a complete reference on Java regular expressions, check the official Java documentation for the java.util.regex package.