package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(<p>|<div .*>)(\s*)(<a .*>)?(\s*)(<img .* \/>)(\s*)(<\/a>)?(\s*)(<p .*>.*<\/p>)?(\s*)(<\/p>|<\/div>)`)
var str = []byte(`<div id="attachment_3340" style="width: 865px" class="wp-caption alignnone">
<img class="size-full wp-image-3340" alt="Testbild" src="https://beispiel.de/images/Testbild.jpg" width="855" height="631" />
<p class="wp-caption-text">© Copyright</p>
</div>`)
var substitution = []byte("$3$5$7$9")
var count = 1 // negative counter is equivalent to global case (replace all)
str = re.ReplaceAllStringFunc(str, func(s string) string {
if count == 0 {
return s
}
count -= 1
return re.ReplaceAllString(s, substitution)
})
fmt.Println(string(str))
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/