import re
regex = re.compile(r"\s(\w+)\s+\[?\]?(\w)(\w+)\s+`json:\"\w+\"`", flags=re.MULTILINE)
test_str = (" Author Author `json:\"author\"`\n"
" Footer Footer `json:\"footer\"`\n"
" Title string `json:\"title\"`\n"
" Description string `json:\"description\"`\n"
" Thumbnail Image `json:\"thumbnail\"`\n"
" Image Image `json:\"image\"`\n"
" URL string `json:\"url\"`\n"
" Fields []Field `json:\"fields\"`\n"
" Color int64 `json:\"color\"`")
subst = "func (b *EmbedB) $1(\\L$2\\E $2$3) *EmbedB {\\n\\tb.Embed.$1$2 = $2\\n\\treturn b\\n}"
result = regex.sub(subst, test_str)
if result:
print(result)
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 Python, please visit: https://docs.python.org/3/library/re.html