Regular Expressions in Logo

Our program demonstrates common regular expression tasks 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.
        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 = r.matcher("peach punch");
        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");
        m.find();
        System.out.println("[" + m.group() + " " + m.group(1) + "]");

        // To find all matches in the input, 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 callback 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 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 matching operations.

Unlike Go, Java doesn’t have separate methods for string and byte slice operations. Instead, all operations are performed on strings, and you can convert between strings and byte arrays using String.getBytes() and new String(byte[]) if needed.

Java’s regex API is slightly different from Go’s. For example, instead of separate methods like FindString, FindStringSubmatch, etc., Java uses the find() method to locate matches and then provides methods like group() to extract information about the matches.

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