I understand your $$...$$
pattern must occupy the whole line (with or without leading/trailing whitespaces).
In this case, you do not need a regex, you can use
s = "$$mystring1$$ \n $$mystring2$$ \n $$fake"
output = []
for l in s.splitlines():
l = l.strip()
if l.startswith('$$') and l.endswith('$$'):
output.append('$$\n' + l[2:-2] + '\n$$')
else:
output.append(l.strip())
print('\n'.join(output))
See this Python demo. That is, if a line starts and ends with $$
, get the line part without the first and last two chars and wrap it with newlines, else, just keep the line as is.
Output:
$$
mystring1
$$
$$
mystring2
$$
$$fake
If you need to break the lines at doubled $$
s, you can use something like
import re
s = "$$mystring1$$ \n $$mystring2$$ \n $$fake"
print(re.sub(r'(\$\$)(.*?)(\$\$)', '\\1\n\\2\n\\3', s))
See this Python demo. Output:
$$
mystring1
$$
$$
mystring2
$$
$$fake
The (\$\$)(.*?)(\$\$)
matches $$
and captures it into Group 1, then captures into Group 2 any zero or more chars other than line break chars as few as possible, and then captures into Group 3 a $$
substring. The replacement pattern just keeps these groups inserting line feeds between Group 1 and 2 and Group 2 and 3.
re.sub(r'(?<=\$\$)(.*?)(?=\$\$)', r"\n\1\n", s)