fork download
  1. import re
  2.  
  3. pattern = r"^(\$\$[\s\S]*?\$\$)$|(?<!\$)\$(\$[\s\S]*?\$)\$(?!\$)"
  4. test_str = ("$$ stand alone $$\n\n"
  5. "$$ stand \n"
  6. "alone $$\n\n"
  7. "And there $$ inline $$. ")
  8.  
  9. regex = re.compile(pattern, re.MULTILINE)
  10. result = re.sub(
  11. regex,
  12. lambda x: x.group(2) if x.group(2) else x.group(1), test_str
  13. )
  14.  
  15. if result:
  16. print (result)
Success #stdin #stdout 0.02s 9536KB
stdin
Standard input is empty
stdout
$$ stand alone $$

$$ stand 
alone $$

And there $ inline $.