Details
-
Improvement
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
None
-
None
Description
Add String#getAt(Pattern, int) and String#getAt(String, int) extension methods.
This will be similar to
StringGroovyMethods.java
public static String getAt(String self, Pattern pattern, int index) { Matcher matcher = pattern.matcher(self); if (!matcher.find()) { return null; } return matcher.group(index + 1); /* I think adding 1 is required, it may not be though to mimic Ruby */ } public static String getAt(String self, String pattern, int index) throws PatternSyntaxException { return getAt(self, Pattern.compile(pattern), index); }
Usage.groovy
assert "Hello".[/(H)ello/, 0] == "H"
The goal of this is to mimic Ruby's Behavior.
NOTICE: I probably have an error in my implementation, but hopefully you get the gist of it.
ALSO
We could also add named capturing groups support (aka getAt(Pattern, String))