From b63b4d1352b2955ac285e337b2e2186f55073f92 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 26 Mar 2025 12:06:04 +0100 Subject: [PATCH] Add some more shorthands for regex --- processor/regex.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/processor/regex.go b/processor/regex.go index 3ff406f..efa27ec 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -74,7 +74,21 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr log.Printf("Pattern modified to include (?s): %s", pattern) } - pattern = strings.ReplaceAll(pattern, "!num", `(\d*\.?\d+)`) + pattern = strings.ReplaceAll(pattern, "!num", `"?(\d*\.?\d+)"?`) + pattern = strings.ReplaceAll(pattern, "!any", `.*?`) + repPattern := regexp.MustCompile(`!rep\(([^,]+),\s*(\d+)\)`) + // !rep(pattern, count) repeats the pattern n times + // Inserting !any between each repetition + pattern = repPattern.ReplaceAllStringFunc(pattern, func(match string) string { + parts := repPattern.FindStringSubmatch(match) + if len(parts) != 3 { + return match + } + repeatedPattern := parts[1] + count := parts[2] + repetitions, _ := strconv.Atoi(count) + return strings.Repeat(repeatedPattern+".*?", repetitions-1) + repeatedPattern + }) compiledPattern, err := regexp.Compile(pattern) if err != nil {