package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?<sub_all>
(?:
(?<!\\)\~
(?<sub_text>
(?>\\[\~[:blank:]\h]|[^\~[:blank:]\h\v])*+
)
(?<!\\)\~
)
|
(?: # or the Microsoft way. Beurk
\<sub\>
(?<sub_text>
((?!\v).)+
)
\<\/sub\>
)
)`)
var str = `log~10~100 is 2.
Space is not allowed:
P~a cat~
But it's ok if it is escaped
P~a\ cat~
Line break is a no-no, even escaped
P~a\
cat~
Hmm <sub>This is a Microsoft subscript!</sub>
H~2~0
text~a\ superscript~
`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
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/