fork download
  1. import re
  2. regex = r"\*_____________________________([\s\S]*?)\*_____________________________(?:\n\*){2}"
  3. test_str = ("*_____________________________\n"
  4. "* This is header text\n"
  5. "* For details, see foobar.txt.\n"
  6. "*_____________________________\n"
  7. "*\n"
  8. "*\n\n"
  9. "* Code goes here\n"
  10. "Code = x\n")
  11. subst = "*\\n* Hello, world.\\n*"
  12.  
  13. # You can manually specify the number of replacements by changing the 4th argument
  14. result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
  15.  
  16. if result:
  17. print (result)
Success #stdin #stdout 0.04s 9664KB
stdin
Standard input is empty
stdout
*
* Hello, world.
*

* Code goes here
Code = x