# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"({.*?})|#.*"
test_str = (" This is 1. # Comment 1.\n"
" This is 2. # Comment 2\n\n"
" #Comment 3 {#Commit4}\n"
" This is 3.\n\n"
" # Commit5\n\n"
" This is 4.{#This is 5} # Commit6\n\n"
" #Commit7\n\n"
" This is 6.\n\n"
" # Commit8 #Commit9\n"
" #Commit10\n"
" # Commit11\n"
" #Commit12 #Commit13\n"
" { # This is 7}; { # This is 8 } # Commit14\n"
" {# This is 9}; { # This is 10 }={# This is 11} {# This is 12}={# This is 13}# Commit15\n"
" \n"
" # Commit16 {# Commit17 }")
subst = "\\1"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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