# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\s*)(.*),?\n"
test_str = ("MERGE\n"
" market-data-351917.cobi_live_analytics.staging_blackrock_benchmark_security AS TARGET\n"
"USING\n"
" (\n"
" SELECT\n"
" security_id,\n"
" effective_begin_timestamp,\n"
" etl_job_id,\n"
" record_digest,\n"
" benchmark_source_name,\n"
" corporate_benchmark_flag,\n"
" corporate_benchmark_weight,\n"
" investment_grade_flag,\n"
" high_yield_flag\n"
" FROM\n"
" market-data-351917.cobi_live_analytics.security_corporate_benchmark_attributes AS a\n"
" LEFT JOIN (\n"
" SELECT\n"
" security_id,\n"
" corporate_benchmark_effective_begin_timestamp,\n"
" corporate_benchmark_record_digest\n"
" FROM\n"
" market-data-351917.cobi_live_analytics.security_current) AS b\n"
" USING\n"
" (security_id)\n"
" WHERE\n"
" a.corporate_benchmark_flag = TRUE\n"
" AND a.effective_begin_timestamp = b.corporate_benchmark_effective_begin_timestamp\n"
" AND a.benchmark_source_name = 'us_corporate_bond_ucits_etf') AS SOURCE\n"
"ON\n"
" target.security_id = source.security_id\n"
" WHEN NOT MATCHED\n"
" THEN\n"
"INSERT\n"
" (security_id,\n"
" effective_begin_timestamp,\n"
" etl_job_id,\n"
" corporate_benchmark_record_digest,\n"
" benchmark_source_name,\n"
" corporate_benchmark_flag,\n"
" corporate_benchmark_weight,\n"
" investment_grade_flag,\n"
" high_yield_flag)\n"
"VALUES\n"
" (source.security_id, source.effective_begin_timestamp, source.etl_job_id, source.record_digest, source.benchmark_source_name, FALSE, source.corporate_benchmark_weight, source.investment_grade_flag, source.high_yield_flag)\n")
subst = "$1\"$2 \"\\\\\\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