Regular Expressions in Minitab

Our first program demonstrates the use of 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, 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 newText = r.matcher("a peach").replaceAll("<fruit>");
        System.out.println(newText);

        // The replaceAll method with a lambda allows you to transform matched text with a given function.
        newText = r.matcher("a peach").replaceAll(matchResult -> matchResult.group().toUpperCase());
        System.out.println(newText);
    }
}

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

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.

Some key differences from other languages:

  1. Java uses Pattern.compile() to create a regex object, similar to other languages’ Compile function.
  2. The Matcher class provides methods like find(), group(), and start()/end() for working with matches.
  3. Java doesn’t have a direct equivalent to Go’s FindAllString. Instead, you can use a while loop with find() to get all matches.
  4. The replaceAll method can be used for string replacement, and it can take a lambda function for more complex replacements.

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