Closed mildred closed 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.
There is a function that takes a named group name and returns the matched text:
func group(m: RegexMatch; groupName: string; text: string): seq[string]
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:
func group(m: RegexMatch; groupIndex: int; text: string): seq[string]
func groupFirstCapture(m: RegexMatch; groupIndex: int; text: string): string
func groupLastCapture(m: RegexMatch; groupIndex: int; text: string): string
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.