With a regex match match, I know I can get the range of the entire match with match.range.
If there is a capture group (say at index 1), I can get the range of the capture group with:
let range = match.1.output.startIndex..< match.1.output.endIndex.
Is there a less verbose way to get the range of a capture group?
Unfortunately not. But I have used this extension on Collection to get it.
extension Collection {
var indexRange: Range<Index> {
startIndex..<endIndex
}
}
Then Substring gets this, too, and match.1.output.indexRange gets you want you want.
1 Like
Thanks @jonathanpenn, also good to know I wasn't missing something.
Danny
4
Alternatively,
let range = Range(match.output.1.indices)
extension Range {
init<Collection: Swift.Collection>(_ indices: DefaultIndices<Collection>)
where Collection.Index == Bound {
self = indices.startIndex..<indices.endIndex
}
}
3 Likes