Triple-Quoted String (Python / TS) in PY
Match triple-quoted strings (Python docstrings, TypeScript triple-quote, etc.) including newlines.
Try it in the PY tester →Pattern
regexPY
"""([\s\S]*?)""" (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"\"\"\"([\\s\\S]*?)\"\"\"")
input_text = "def foo():\\n \"\"\"Docstring here.\"\"\"\\n pass"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
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
—