# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\n\n|\n(?=[^\n]*\n\n)"
test_str = ("Report Area\n\n"
"Total Population\n\n"
"Total Land Area\n"
"(Square Miles)\n\n"
"Population Density \n"
"(Per Square Mile)\n\n\n"
"Report Area 37,325,068 155,738.02 239.67 \n"
"Alameda County, CA 1,515,136 738.82 2,050.75 \n"
"Alpine County, CA 1,197 738.13 1.62 \n"
"Amador County, CA 37,764 594.43 63.53 \n"
"Butte County, CA 220,101 1,636.03 134.53 \n"
"Calaveras County, CA 45,507 1,019.74 44.63 \n"
"Colusa County, CA 21,329 1,150.43 18.54 ")
subst = " "
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.IGNORECASE)
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