Add support for !num inside of named capture groups

This commit is contained in:
2025-03-26 14:04:39 +01:00
parent a9b6f7f984
commit bd443067b6
2 changed files with 48 additions and 1 deletions

View File

@@ -80,6 +80,20 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
log.Printf("Pattern modified to include (?s): %s", pattern)
}
// The order of these replaces is important
// This one handles !num-s inside of named capture groups
// If it were not here our !num in a named capture group would
// Expand to another capture group in the capture group
// We really only want one (our named) capture group
namedGroupNum := regexp.MustCompile(`(?:(\?<[^>]+>)(!num))`)
pattern = namedGroupNum.ReplaceAllStringFunc(pattern, func(match string) string {
parts := namedGroupNum.FindStringSubmatch(match)
if len(parts) != 3 {
return match
}
replacement := `\d*\.?\d+`
return parts[1] + replacement
})
pattern = strings.ReplaceAll(pattern, "!num", `"?(\d*\.?\d+)"?`)
pattern = strings.ReplaceAll(pattern, "!any", `.*?`)
repPattern := regexp.MustCompile(`!rep\(([^,]+),\s*(\d+)\)`)
@@ -125,7 +139,7 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
log.Printf("Error creating Lua state: %v", err)
return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err)
}
// Hmm... Maybe we don't want to defer this..
// Hmm... Maybe we don't want to defer this..
// Maybe we want to close them every iteration
// We'll leave it as is for now
defer L.Close()