Triple-Quoted String (Python / TS) in GO
Match triple-quoted strings (Python docstrings, TypeScript triple-quote, etc.) including newlines.
Try it in the GO tester →Pattern
regexGO
"""([\s\S]*?)""" (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`"""([\s\S]*?)"""`)
input := `def foo():\n """Docstring here."""\n pass`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
The opening and closing """ bracket the string. ([\s\S]*?) is the lazy any-character-including-newline pattern: [\s\S] is the universal-character idiom (avoids needing the s/dotAll flag), and *? keeps the match minimal so adjacent triple-quote blocks don't merge.
Examples
Input
def foo():\n """Docstring here."""\n passMatches
"""Docstring here."""
Input
a = """line1\nline2""" b = """third"""Matches
"""line1\nline2""""""third"""
Input
no triple quotesNo match
—