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.5.
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 downloadnamed-regexp-0.2.5.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.5</version>
</dependency>
docs
Check out the javadocs
notes;
-
0.2.5 / JUN 23, 2017
- Fixed
Matcher#namedGroups()to return all matching subsequences
- Fixed
-
0.2.4 / JUN 27, 2016
- Fixed serialization issues
Bug
- [REGEX-23] - Pattern is not Serializable
Improvement
-
Expose
Patternconstants (i.e.,CASE_INSENSITIVE,MULTILINE, etc.)
-
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)
- Fixed invalid group value when pattern included
-
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
- Fixed invalid group value when pattern included parentheses in character classes (i.e.,
-
0.2.1 / AUG 12, 2013
- Removed deprecated classes (
NamedPattern,NamedMatcher,NamedMatchResult). UsePattern,Matcher, andMatchResultinstead.
Improvement
- [REGEX-19] - Removed deprecated "Named" classes
- Removed deprecated 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
NamedPatternandNamedMatchertoPatternandMatcher, 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
- Renamed
-
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
- Fixed invalid group value when pattern included literal slashes (i.e.,
-
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
- Fixed invalid group value when pattern included look-behinds (i.e.,
-
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
- Fixed invalid group value when pattern included escaped parentheses (i.e.,
-
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
- Fixed invalid group value when pattern included special constructs (i.e.,
-
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
Patternclass - Fixed
IndexOutOfBoundsExceptionwhen getting a map of named groups in the pattern
- Fixed invalid group value when pattern contained unnamed groups (i.e.,
-
0.1.1 / JUL 14, 2012
- Initial commit (forked from Google code)