# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:\A|;)(?:\s*)((?:%macro |%inc(?:lude)?\b|data |proc )[^;]+)"
test_str = ("%macro foo;\n"
" %inc \"foo.sas\";\n"
"%mend;\n\n"
"%macro mylogit1(all_deps, outest);\n"
" %let k=1;\n"
" %let dep = %scan(&all_deps, &k);\n"
" %do %while(\"&dep\" NE \"\");\n"
" title \"dependent variable is &dep\";\n"
" proc logistic data=xxx des outest=_est&k;\n"
" model &dep = ind1 ind2;\n"
" run;\n"
" %let k = %eval(&k + 1);\n"
" %let dep = %scan(&all_deps, &k);\n"
" %end;\n"
" %if \"&outest\" NE \"\" %then \n"
" %do;\n"
" data &outest;\n"
" set \n"
" %do i = 1 %to &k - 1;\n"
" _est&i\n"
" %end; \n"
" ;\n"
" run; \n"
" %let k = %eval(&k - 1);\n"
" proc datasets;\n"
" delete _est1 - _est&k;\n"
" run;\n"
" %end;\n"
" %else \n"
" %do;\n"
" %put no dataset name was provided, files are not combined;\n"
" %end;\n"
"%mend;\n"
"%mylogit1(v1 v2 v3);\n\n"
"%mylogit1(v1 v2 v3, a);\n"
"proc print data = a;\n"
" var intercept ind1 ind2;\n"
"run;\n")
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
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