Hi all

I have a big PCRE regex, that have conditional named groups. I want to 
reference all of the groups, even if match didn't occur and handle it on my 
side with simple if or something like this.

As nre [documentation](https://nim-lang.org/docs/nre.html) states: captures[]: 
string the string value of whatever was captured at that id. If the value is 
invalid, then behavior is undefined. If the id is -1, then the whole match is 
returned. If the given capture was not matched, nil is returned. 
    
    
    "abc".match(re"(\w)").get.captures[0] == "a"
    "abc".match(re"(?<letter>\w)").get.captures["letter"] == "a"
    "abc".match(re"(\w)\w").get.captures[-1] == "ab"
    
    
    Run

I can do that and get nil, if pattern was not matched. But actual nre code 
contains this: 
    
    
    func checkNamedCaptured(pattern: RegexMatch, name: string): void =
      if not (name in pattern.captureBounds):
        raise newException(KeyError, "Group '" & name & "' was not captured")
    
    ...
    
    func `[]`*(pattern: Captures, name: string): string =
      let pattern = RegexMatch(pattern)
      checkNamedCaptured(pattern, name)
      return pattern.captures[pattern.pattern.captureNameToId[name]]
    
    
    Run

Which throws error if named group was not matched. 

Reply via email to