This library is a wrapper for java.util.regex
, implementing named capture groups for Java 5/6. Note this isn't needed in Java 7, which already supports this feature.
This is a fork of the named-regexp project that fixes several bugs.
- Replace
java.util.regex.Pattern
andjava.util.regex.Matcher
withNamedPattern
andNamedMatcher
, respectively. - Use
(?<name>...)
to specify a named capture group as in the following code example:
public static void main(String[] args) { String input = "hello world!"; String regex = "(?<foo>world)"; // contains capture group named "foo" boolean found; NamedPattern pattern = NamedPattern.compile(regex); NamedMatcher matcher = pattern.matcher(input); found = matcher.find(); System.out.println("Input: " + input); System.out.println("Regex: " + regex); System.out.println("Found: " + found); if (found) { System.out.println("Captured: " + matcher.group("foo")); } }