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.1.9.
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.1.9.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.1.9</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.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)