import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<prm>\\w+)\\s+.*:\\s*=\\s*'(?<tbl>\\w+)'\\s*;(?:.|\\n)*(?:alter|create)\\s+table\\s+'\\s*\\|\\|\\s*(\\k<prm>\\s+)";
final String string = "DECLARE\n"
+ "COUNTER NUMBER(10) := 0;\n"
+ "pTABLENAME VARCHAR2(100) := 'ALM_AL1';\n"
+ "pCOLUMNNAME VARCHAR2(100) := 'FLOATACCRUALDCBASISDAYC';\n"
+ "pDATATYPE VARCHAR2(100) := 'NVARCHAR2(50) NULL';\n"
+ "pSQL VARCHAR2(32000) := '\n"
+ "ALTER TABLE ' || pTABLENAME || ' ADD ' || pCOLUMNNAME || ' ' || pDATATYPE;\n"
+ "BEGIN\n"
+ " SELECT COUNT(0) INTO COUNTER FROM USER_TAB_COLUMNS WHERE TABLE_NAME=UPPER(pTABLENAME) AND COLUMN_NAME=UPPER(pCOLUMNNAME);\n"
+ " IF COUNTER>0 THEN\n"
+ " dbms_output.put_line(pTABLENAME || ' tablosunda ' || pCOLUMNNAME || ' kolonu var.');\n"
+ " ELSE\n"
+ " EXECUTE IMMEDIATE pSQL;\n"
+ " dbms_output.put_line(pTABLENAME || ' tablosuna' || pCOLUMNNAME || ' kolonu oluÅ?turuldu.');\n"
+ " END IF;\n"
+ " \n"
+ " EXCEPTION\n"
+ " WHEN OTHERS THEN\n"
+ " dbms_output.put_line('HATA: ' || pSQL || ' -> SQLERRM: ' || SQLERRM);\n"
+ "END;\n"
+ "/\n\n"
+ "DECLARE\n"
+ "COUNTER NUMBER(10) := 0;\n"
+ "pTABLENAME VARCHAR2(100) := 'ALM_AL2';\n"
+ "pCOLUMNNAME VARCHAR2(100) := 'RATETYPETYPE';\n"
+ "pDATATYPE VARCHAR2(100) := 'NVARCHAR2(40) NULL';\n"
+ "pSQL VARCHAR2(32000) := '\n"
+ "ALTER TABLE ' || pTABLENAME || ' ADD ' || pCOLUMNNAME || ' ' || pDATATYPE;\n"
+ "BEGIN\n"
+ " SELECT COUNT(0) INTO COUNTER FROM USER_TAB_COLUMNS WHERE TABLE_NAME=UPPER(pTABLENAME) AND COLUMN_NAME=UPPER(pCOLUMNNAME);\n"
+ " IF COUNTER>0 THEN\n"
+ " dbms_output.put_line(pTABLENAME || ' tablosunda ' || pCOLUMNNAME || ' kolonu var.');\n"
+ " ELSE\n"
+ " EXECUTE IMMEDIATE pSQL;\n"
+ " dbms_output.put_line(pTABLENAME || ' tablosuna' || pCOLUMNNAME || ' kolonu oluÅ?turuldu.');\n"
+ " END IF;\n"
+ " \n"
+ " EXCEPTION\n"
+ " WHEN OTHERS THEN\n"
+ " dbms_output.put_line('HATA: ' || pSQL || ' -> SQLERRM: ' || SQLERRM);\n"
+ "END;\n"
+ "/\n\n"
+ "DECLARE\n"
+ "COUNTER NUMBER(10) := 0;\n"
+ "pTABLENAME VARCHAR2(100) := 'ALM_AL3';\n"
+ "pCOLUMNNAME VARCHAR2(100) := 'PAYCURRENCYUNIT';\n"
+ "pDATATYPE VARCHAR2(100) := 'NVARCHAR2(3) NULL';\n"
+ "pSQL VARCHAR2(32000) := '\n"
+ "ALTER TABLE ' || pTABLENAME || ' ADD ' || pCOLUMNNAME || ' ' || pDATATYPE;\n"
+ "BEGIN\n"
+ " SELECT COUNT(0) INTO COUNTER FROM USER_TAB_COLUMNS WHERE TABLE_NAME=UPPER(pTABLENAME) AND COLUMN_NAME=UPPER(pCOLUMNNAME);\n"
+ " IF COUNTER>0 THEN\n"
+ " dbms_output.put_line(pTABLENAME || ' tablosunda ' || pCOLUMNNAME || ' kolonu var.');\n"
+ " ELSE\n"
+ " EXECUTE IMMEDIATE pSQL;\n"
+ " dbms_output.put_line(pTABLENAME || ' tablosuna' || pCOLUMNNAME || ' kolonu oluÅ?turuldu.');\n"
+ " END IF;\n"
+ " \n"
+ " EXCEPTION\n"
+ " WHEN OTHERS THEN\n"
+ " dbms_output.put_line('HATA: ' || pSQL || ' -> SQLERRM: ' || SQLERRM);\n"
+ "END;\n"
+ "/";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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