View on GitHub

(?<named>regexp+)

Named capture groups for Java 5/6 regular expressions

Download named-regexp-0.2.3.jar

about!

named-regexp is a thin wrapper for good ol' java.util.regex with support for named capture groups in Java 5/6. (yes, Java 7 already does this...)

The current version is 0.2.3.

snippet.

The constructs for named groups and references are the same as in Java 7.

named groups

To create a named capture group, use (?<name>...), as in:
Pattern p = Pattern.compile("abc(?<foo>\\d+)xyz");
Matcher m = p.matcher("abc123xyz");
System.out.println(m.find());       // true
System.out.println(m.group("foo")); // 123

back references

To back-reference a named group in a pattern, use \k<name>, as in:
Pattern p = Pattern.compile("abc(?<foo>\\d+)def\\k<foo>");
Matcher m1 = p.matcher("abc123def123ghi");
Matcher m2 = p.matcher("abc123def456ghi");
System.out.println(m1.find());       // true
System.out.println(m1.group("foo")); // 123
System.out.println(m2.find());       // false

replacement references

To reference a named group for string replacement, use ${name}, as in:
Pattern p = Pattern.compile("abc(?<foo>\\d+)def");
Matcher m = p.matcher("abc123def");
System.out.println(m.replaceAll("${foo}456 ${foo}")); // 123456 123

download$

You can download named-regexp-0.2.3.jar and add it to your classpath; or include it as a Maven dependency:
<dependency>
  <groupId>com.github.tony19</groupId>
  <artifactId>named-regexp</artifactId>
  <version>0.2.3</version>
</dependency>

help?

Feel free to email named-regexp@googlegroups.com.
Please report any bugs or feature requests in JIRA.
Also check out the javadocs.

notes;

  • 0.2.3 / SEP 15, 2013

    • Fixed invalid group value when pattern included \Q-escaped brackets or parentheses (i.e., \Q (abc [ \E(?<named>)x \Q ] ) \E)

    Bug

    • [REGEX-22] - Wrong groups values when pattern contains quoted bracket

    Improvement

    • [REGEX-21] - Indicate group name in exception when not found
  • 0.2.2 / AUG 28, 2013

    • Fixed invalid group value when pattern included parentheses in character classes (i.e., [()a-f]+(?<named>x))

    Bug

    • [REGEX-20] - Named group index is incorrect after parens in a character class
  • 0.2.1 / AUG 12, 2013

    • Removed deprecated classes (NamedPattern, NamedMatcher, NamedMatchResult). Use Pattern, Matcher, and MatchResult instead.

    Improvement

    • [REGEX-19] - Removed deprecated "Named" classes
  • 0.1.10 / AUG 12, 2013

    • Removed character restriction in group names so that they can now contain most free-form text (including Unicode characters or whitespace). However, the name must not begin with ! (exclamation point) or = (equal symbol) since those characters would create a look-around construct.

    Improvement

    • [REGEX-18] - Remove character restriction in group names
  • 0.1.9 / DEC 28, 2012

    • Renamed NamedPattern and NamedMatcher to Pattern and Matcher, respectively. The old classes have been deprecated and will be removed in a future release.
    • Added support for named back-references and string replacements
    • Now works on Android!

    Bug

    • [REGEX-8] - NamedMatcher.namedGroups() returns unnamed groups
    • [REGEX-9] - Android: NamedPattern.compile() gets stuck in infinite loop
    • [REGEX-10] - NamedMatcher.equals() does not compare all instance fields with another; hashCode() also wrong

    Improvement

    • [REGEX-13] - Remove "Named" prefix from class names

    New Feature

    • [REGEX-11] - Implement back references for named groups
    • [REGEX-12] - Implement named references in string replacement
  • 0.1.8 / JUL 18, 2012

    • Fixed invalid group value when pattern included literal slashes (i.e., \\(notEscapedParen)(?<foo>\d+))

    Bug

    • [REGEX-6] - Capture groups ignored if preceded by literal slash
  • 0.1.7 / JUL 16, 2012

    • Fixed invalid group value when pattern included look-behinds (i.e., abc(?<=lookbehind)(?<foo>\d+))

    Bug

    • [REGEX-14] - Invalid group value when pattern included lookarounds
  • 0.1.6 / JUL 15, 2012

    • No code changes
    • Uploaded project to Maven Central

    Task

    • [REGEX-4] - Upload to central maven repository
  • 0.1.5 / JUL 15, 2012

    • No code changes
  • 0.1.4 / JUL 15, 2012

    • Fixed invalid group value when pattern included escaped parentheses (i.e., \(notCaptureGroup)(?<foo>\d+))

    Bug

    • [REGEX-16] - Invalid group value when pattern contained escaped parentheses
  • 0.1.3 / JUL 15, 2012

    • Fixed invalid group value when pattern included special constructs (i.e., (?idsumx)(?<foo>\d+))

    Bug

    • [REGEX-15] - Invalid group value when pattern includes special constructs
  • 0.1.2 / JUL 14, 2012

    • Fixed invalid group value when pattern contained unnamed groups (i.e., (\s+unnamed)(?<foo>\d+))
    • Fixed bug that hid regex flags from underlying Pattern class
    • Fixed IndexOutOfBoundsException when getting a map of named groups in the pattern

    Bug

    • [REGEX-1] - Pattern Flags are ignored
    • [REGEX-2] - Unnamed capture groups are broken
    • [REGEX-3] - nested group
    • [REGEX-7] - NamedMatcher.namedGroups() causes IndexOutOfBoundsException
  • 0.1.1 / JUL 14, 2012