Regular Expressions in Mercury

Our first example demonstrates how to use regular expressions in Java. Here’s the full source code:

import java.util.regex.*;

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.
        System.out.println(r.matcher("peach").matches());

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

        // This also finds the first match but returns the start and end indexes for the match.
        m = r.matcher("peach punch");
        if (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 = r.matcher("peach punch");
        if (m.find()) {
            System.out.println(m.group() + " " + m.group(1));
        }

        // To find all matches in the input, not just the first, use a while loop.
        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 with a lambda allows you to transform matched text with a given function.
        result = r.matcher("a peach").replaceAll(matchResult -> matchResult.group().toUpperCase());
        System.out.println(result);
    }
}

To run the program, compile and execute it using the javac and java commands:

$ javac RegularExpressions.java
$ java RegularExpressions
true
true
peach
idx: [0 5]
peach ea
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.

The matches method of the Pattern class is used for simple pattern matching, similar to Go’s MatchString.

The find method of the Matcher class is used to find the next match in the input sequence, similar to Go’s FindString.

Java doesn’t have direct equivalents for all of Go’s regex methods, but you can achieve similar functionality using combinations of Pattern and Matcher methods.

The group methods in Java provide similar functionality to Go’s Submatch variants, allowing you to access captured groups.

Java’s replaceAll method is similar to Go’s ReplaceAllString, and can be used with a lambda function to provide functionality similar to Go’s ReplaceAllFunc.

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