J% module
Regex
Regular expressions compiled and checked when you build, not when the first input reaches them.
- Base type
- org.jmod.dsl.regex.Regex
- Configuration
- RegexConfiguration
Write the pattern as the body of an external type and the compiler checks it while it builds. A malformed expression fails the build; the class you get back wraps a java.util.regex pattern that was compiled once.
Writing one
package examples.simpleregex;
import org.jmod.dsl.regex.Regex;
import org.jmod.dsl.regex.RegexConfiguration;
public external IpAddress extends Regex<RegexConfiguration> {
([0-9]{1,3}\\.){3}[0-9]{1,3}
}Then use it as an ordinary Java class:
IpAddress ip = new IpAddress();
ip.matches("127.0.0.1"); // whole-input match
ip.find(line); // State.FOUND / NOT_FOUND
ip.group(); // the last match
ip.replaceAll("x.x.x.x");What you get told at build time
BadRegex.jmod: invalid regular expression: Illegal character range near index 15
That is the whole check, and the build stops. There is one setting, REGEX_ENGINE, and it accepts only jdk.
Worth knowing
A regex external is a constant: it takes no host values, and a #[name]<Type> written inside the pattern is treated as pattern text rather than a parameter. If you need values spliced in, this is the wrong module — see SQL or JSON.
The body ends at the brace that closes it, so a pattern containing a lone { or } confuses the parser. Balanced quantifiers like {1,3} are fine.