import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(^\\|(\\s*[A-Z][A-Z_]*\\s*\\|)+\\s*$)\\r?\\n(^\\|([\\s-]+\\|)+\\s*$)";
final String string = "Which SQL query will help you fetch the department ID and department name for departments where the number of employees exceeds 10?\n"
+ "| EMP_ID | F_NAME | L_NAME | SALARY | DEPT_ID_DEP |\n"
+ "| --- | --- | --- | --- | --- |\n"
+ "| 1 | John | Doe | 60000 | 101 |\n"
+ "| 2 | Jane | Smith | 75000 | 102 |\n"
+ "| 3 | Emily | Davis | 80000 | 101 |\n"
+ "| 4 | Michael | Johnson | 65000 | 103 |\n"
+ "| DEP_ID | DEP_NAME | MANAGER_ID | LOCATION_ID |\n"
+ "| --- | --- | --- | --- |\n"
+ "| 101 | IT | 10 | L0001 |\n"
+ "| 102 | HR | 20 | L0002 |\n"
+ "| 103 | Finance | 30 | L0003 |\n"
+ "| 104 | Marketing | 40 | L0004 |";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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