package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?i)(<a([^>]*)>)(<img([^>]*)>)`)
var str = []byte(`<a href="http://www.mobil.hr/wp-content/uploads/2017/12/Screenshot_20171219-102229.png"><img class="aligncenter wp-image-4054" src="http://www.mobil.hr/wp-content/uploads/2017/12/Screenshot_20171219-102229-563x1000.png" alt="" width="339" height="602" /></a>`)
var substitution = []byte("<div>\1 \3</div>")
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/