Regular Expressions in Fortress

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

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

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

        // To find all matches for a regex, 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 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 the code into a .class file 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 
a <fruit>
a PEACH

This example demonstrates various operations with regular expressions in Java:

  1. Simple pattern matching
  2. Compiling patterns
  3. Finding matches
  4. Extracting submatches
  5. Finding all matches
  6. Replacing matched text

Note that Java’s regex API is somewhat different from Go’s. Instead of having many methods on a single object, Java splits functionality between Pattern and Matcher classes. The Pattern class represents a compiled regular expression, while the Matcher class is used to perform match operations on a character sequence by interpreting a Pattern.

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