Regular Expressions in Miranda

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

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

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.
        m = r.matcher("peach punch");
        if (m.find()) {
            System.out.println("[" + m.group() + " " + m.group(1) + "]");
        }

        // To find all matches in the input, we can 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 also allows you to transform matched text with a given function.
        result = r.matcher("a peach").replaceAll(mr -> mr.group().toUpperCase());
        System.out.println(result);
    }
}

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

$ 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.

Unlike Go, Java doesn’t have separate methods for string and byte slice operations. Instead, all operations are performed on character sequences (usually strings).

The matches method of Pattern is equivalent to Go’s MatchString. For other operations, you typically compile a Pattern and then create a Matcher object to perform operations.

Java’s regex API doesn’t provide direct equivalents for all of Go’s methods (like FindStringSubmatchIndex), but you can achieve similar results by combining existing methods.

The replaceAll method in Java is similar to Go’s ReplaceAllString. Java also allows you to use a function (via a lambda expression) to perform replacements, similar to Go’s ReplaceAllFunc.

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