Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
b3fce4244d | |||
bd443067b6 |
@@ -80,7 +80,21 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
|||||||
log.Printf("Pattern modified to include (?s): %s", pattern)
|
log.Printf("Pattern modified to include (?s): %s", pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern = strings.ReplaceAll(pattern, "!num", `"?(\d*\.?\d+)"?`)
|
// 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", `.*?`)
|
pattern = strings.ReplaceAll(pattern, "!any", `.*?`)
|
||||||
repPattern := regexp.MustCompile(`!rep\(([^,]+),\s*(\d+)\)`)
|
repPattern := regexp.MustCompile(`!rep\(([^,]+),\s*(\d+)\)`)
|
||||||
// !rep(pattern, count) repeats the pattern n times
|
// !rep(pattern, count) repeats the pattern n times
|
||||||
|
@@ -581,6 +581,39 @@ func TestNamedCaptureGroups(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNamedCaptureGroupsNum(t *testing.T) {
|
||||||
|
content := `<config>
|
||||||
|
<item>
|
||||||
|
<value>100</value>
|
||||||
|
</item>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
expected := `<config>
|
||||||
|
<item>
|
||||||
|
<value>200</value>
|
||||||
|
</item>
|
||||||
|
</config>`
|
||||||
|
|
||||||
|
p := &RegexProcessor{}
|
||||||
|
result, mods, matches, err := p.ProcessContent(content, `(?s)<value>(?<amount>!num)</value>`, "amount = amount * 2")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error processing content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != 1 {
|
||||||
|
t.Errorf("Expected 1 match, got %d", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mods != 1 {
|
||||||
|
t.Errorf("Expected 1 modification, got %d", mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMultipleNamedCaptureGroups(t *testing.T) {
|
func TestMultipleNamedCaptureGroups(t *testing.T) {
|
||||||
content := `<product>
|
content := `<product>
|
||||||
<name>Widget</name>
|
<name>Widget</name>
|
||||||
|
Reference in New Issue
Block a user