Regular Expressions in Squirrel

Our first program demonstrates the use of regular expressions in Java. Here’s the full source code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.
        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(Arrays.asList(m.group(), m.group(1)));
        }

        // Similarly this will return information about the indexes of matches and submatches.
        m = r.matcher("peach punch");
        if (m.find()) {
            System.out.println(Arrays.asList(m.start(), m.end(), m.start(1), m.end(1)));
        }

        // To find all matches for a regex.
        m = r.matcher("peach punch pinch");
        while (m.find()) {
            System.out.print(m.group() + " ");
        }
        System.out.println();

        // Providing a limit to the number of matches.
        m = r.matcher("peach punch pinch");
        int limit = 2;
        int count = 0;
        while (m.find() && count < limit) {
            System.out.print(m.group() + " ");
            count++;
        }
        System.out.println();

        // The regex package can also 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 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 the code and then use java to execute it:

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

In Java, regular expressions are handled by the java.util.regex package. The Pattern class is used to define a regular expression, and the Matcher class is used to match the pattern against a string.

Unlike Go, Java doesn’t have a direct equivalent to MustCompile. Instead, you can wrap the Pattern.compile() method in a try-catch block if you want to handle compilation errors.

The replaceAll method in Java can take a lambda function to transform matched text, which is similar to the ReplaceAllFunc in Go.

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