# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(.{1,80}\S)(?:[\r\n\f\v ]+|$)"
test_str = ("--- 0---|--- 10---|--- 20---|--- 30---|--- 40---|--- 50---|--- 60---|--- 70---|--- 80---|--- 90---|\n"
"123456789|123456789|123456789|123456789|123456789|123456789|1 23456789|123456789|123456789|123456789|\n\n\n"
"SELECT\n"
" tbl_Hex.Hex_ID\n"
" ,tbl_Generic_Tool.Generic_Code AS [Generic Code]\n"
" ,tbl_ref_Generic_Type.Generic_Type AS [Generic Type]\n"
"FROM tbl_ref_Location\n"
" RIGHT JOIN ((tbl_ref_Generic_Type\n"
" INNER JOIN tbl_Generic_Tool ON tbl_ref_Generic_Type.Generic_Type_ID = tbl_Generic_Tool.Generic_Type_ID)\n"
" INNER JOIN (tbl_ref_Building\n"
" INNER JOIN tbl_Hex ON tbl_ref_Building.Building_ID = tbl_Hex.Building_ID) ON tbl_Generic_Tool.Generic_ID = tbl_Hex.Generic_ID) ON tbl_ref_Location.Location_ID = tbl_Hex.Location_ID\n"
"GROUP BY\n"
" tbl_Hex.Hex_ID\n"
" ,tbl_Hex.Hex_No\n"
" ,tbl_ref_Building.Building_Abbrev\n"
" ,tbl_ref_Location.Location\n"
" ,tbl_Generic_Tool.Generic_Code\n"
" ,tbl_ref_Generic_Type.Generic_Type\n"
"ORDER BY\n"
" tbl_Hex.Hex_No;\n")
subst = "$1\\r\\n"
# 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