import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:\\A|;)(?:\\s*)((?:%macro |%inc(?:lude)?\\b|data |proc )[^;]+)";
final String string = "%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";
final String subst = "";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html