import re
regex = re.compile(r"(?>\\caption(?:of)?\b[^{]*{|(?<!^)\G)(?>[^}{]|(\{(?:[^}{]*+|(?-1))*}))*?\K\\{2}")
test_str = (" \\begin{figure} \n"
" \\includegraphics{pic.pdf}\n"
" \\caption[]{My caption \\\\ \n"
" Source: XYZ}\n"
" \\label{fig:pic_1} \n"
" \\end{figure}\n"
" \n"
" \n"
" \\begin{figure}[H]\n"
" \\includegraphics{pic.pdf}\n"
" \\captionof[]{My caption \\\\ xyz \\\\ abc\n"
" \\label{fig:pic_1} }\n"
" \\end{figure}\n"
" \n"
" \n"
" \\begin{figure}[H]\n"
" \\includegraphics{pic.pdf}\n"
" \\caption[]{My caption {with extra brackets}\n"
" Source: XYZ}\n"
" \\label{fig:pic_1} \n"
" \\end{figure}\n"
" \n"
" \\begin{figure}[H]\n"
" \\includegraphics{pic.pdf}\n"
" \\caption[]{My caption}\n"
" \\end{figure}\n"
" \n"
" Some text\\\\ %% This \\\\ should not be changed, it's not within a caption\n"
" More text\n"
" \n"
" \\begin{figure}[H]\n"
" \\includegraphics{pic.pdf}\n"
" \\caption[]{My caption \\\\ Source: XYZ}\n"
" \\label{fig:pic_1} \n"
" \\end{figure}")
subst = "\\\\par"
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