package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?x) # Free-Spacing
(?(DEFINE) # Define a few subroutines
(?<double>“(?:(?!&[lr]squo;).)*”) # full set of doubles (no quotes inside)
(?<single>‘(?:(?!&[lr]dquo;).)*’) # full set of singles (no quotes inside)
(?<notquotes>(?:(?!&[lr][sd]quo;).)*) # chars that are not quotes
) # end DEFINE
^ # Start of string
( # Start group: balanced portion
(?: # Start non-capture group
(?¬quotes) # Any non-quote chars
&l(?<type>[sd])quo; # Opening quote, capture single or double type
# any full singles, doubles, not quotes or recursion
(?:(?&single)|(?&double)|(?¬quotes)|(?R))*
&r\k<type>quo; # Closing quote of the correct type
)*+ # Repeat non-capture group
) # end balanced portion
\K
(
(?¬quotes)
&r([sd])quo; # unmatched quote
)`)
var str = []byte(`“Full Quote” The Left Quote is Missing”
`)
var substitution = []byte("&l\7quo;\6")
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/