New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
library: Match should extend sequence...or at least have a reasonable behavior with group, [], etc #6622
Comments
It seems reasonable to have a length when it has integer-indexed values. Removed Type-Defect label. |
This comment was originally written by @seaneagan I would say just get rid of "group", "operator []", and "groupCount", and add a "value" getter. Then instead of this: print(match[0]); just do: print(match.value); |
I've also just been confused by this strange API: http://stackoverflow.com/q/25961074/25124 Things that don't make sense to me:
My suggestion (though it'd be a breaking change), is to make the group indexes 0-based, expose the list (or make Match iterable, without the full match), and put the full match as a new property on Match. |
(Oops, the -1 shouldn't be there) |
Any future updates with last mentioned issue? final groups = match.groups(List<int>.generate(match.groupCount, (i) => i + 1)); to: final groups = match.groups(); or get all with first full match. |
The What I want to do right now is to add two extension members: List<String?> get captures => groups.sublist(1);
String get match => this[0]!; This is a minimal API which allows you to stop using the existing (Actually, I'd prefer to only put |
Scenario:
// regex with 4 captures
final match = const RegExp(r'^(\d+)\s+(\w+)\s+(\w+)\s+([\w/]+)');
// groupCount is 4, which seems reasonable at first
for(var i = 0; i< match.groupCount; i++) {
print(match[i]);
}
// but match[0] returns the entire string
// so to see all matches I must
for(var i = 0; i< match.groupCount + 1; i++) {
print(match[i]);
}
This feels very weird.
Also, the duplication of behavior of group and [] is confusing.
I'd vote that Match extend Sequence<String>.
The text was updated successfully, but these errors were encountered: