package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)^(?P<indentation>(?P<sp_or_tab>[ \t])*+)(?P<stmt>(?P<non_str_lit>(?:(?!\#)[^\"'\r\n])*?)(?:(?P<str_lit>(?P<begin_quote>(?P<single_quote>')|\")(?:(?(single_quote)\"|')|\\[\"']|\\[^\r\n ]|[^\"'\\\r\n])*?(?P=begin_quote))(?&non_str_lit))*?)(?P<whitespace>(?&sp_or_tab)*+)(?P<comment>(?:\#)[^\r\n]*+)?(?P<line_ending>\r|\n|\r\n)?$`)
var str = `print("Hello, world!") # this is a comment.
print("The quick \\brown\\\ndog\rfox\tjumps\t over #the# /lazy/ \"dog\"###") # and here's where the real comment starts
print("you can use Alt+0009 to enter a tab character!")
# The above line had no comments, so the <comment> group didn't participate in the match.
print("indented code") # with comment
print("never indent code like this, but it still matches anyway")# comment lacking preceding whitespace >:)
print("space indented code")
print("Missing end quote
print("unescaped \")
print("implicitly" " joined## " "string " "lit#erals") # a line can contain multiple string literals
print("the quick " # line 1
"brown fox " # line 2
"jumps over " # line 3
"the lazy dog") # line 4
print("The quick brown", animal, "jumps over the", adjective, animal2, sep=" ") "
print('Robert'); DROP TABLE \`Students\`; -- ')`
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/