Skip to content

RegEx: add a way to get the positions of groups #42307

@ethanblake4

Description

@ethanblake4

When executing a RegExp, groups are returned as a map of Strings. However, for some use cases it is really important to get the index of where these groups are in the input string, rather than just the text.

For example, I have the following code:

final pattern = RegExp('(?:(?<=[^\\\\])|^){{(\\w*)}}');
final match = pattern.firstMatch('A captured word I {{capture}}');
print(match.group(1));

This correctly prints 'capture'. However, there is no way to know (at least, in my use case where I am being fed regexps from an external file) whether this group is referring to 'capture' in the source string at span 2-10 or 20-28. In this case the answer is 20-28 and that is what I would like to be able to retrieve.

In other languages:
In python you would call match.span(1) which would return a tuple of the start and end position. In Dart this could be replaced with an object.
In JavaScript this is not supported, meaning this feature would probably not be supported in dart2js.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-coretype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions