nitely / nim-regex

Pure Nim regex engine. Guarantees linear time matching
https://nitely.github.io/nim-regex/
MIT License
227 stars 20 forks source link

RFE: Missing a function for RegexMatch that takes a group index and return the matched string #72

Closed mildred closed 4 years ago

mildred commented 4 years ago

There is a function that takes a named group name and returns the matched text:

But there is no similar function that takes numbered group index instead. it would have the following signature. It could be nide to add utiliti functions to get first and last capture too:

It would be nice too if RegexMatch could hold a reference to the matched string so it would not be required to pass it to those functions as additional parameter.

nitely commented 4 years ago

Possibly, but that API was added by someone, and I don't use it. This is how I'd do it:

import regex
let text = "abc"
var m: RegexMatch
doAssert text.match(re"(\w)+", m)
doAssert text[m.group(0)[0]] == "a"  # same as groupFirstCapture
doAssert text[m.group(0)[^1]] == "c"  # same as groupLastCapture

I think better ergonomics should be solved with macros.