Unless the text always comes in a very regular fashion, regex is not a suitable for parsing source code like this. You should write/use a parser instead.
Assuming:
- There will be no DECLARE statements between CREATE FUNCTION and BEGIN
- There will not be any other statements in between the DECLARE statements
- There will always be at least one DECLARE statement
You can use a regex like this:
([\s\S]+?)((?:\s+DECLARE.+)+)([\s\S]+)
Note that this can cause catastrophic backtracking if there are no DECLARE statements. If the input could have no DECLARE statements, you could just match the (?:\s+DECLARE.+)+
part, and get the other two groups with substring:
String input = "CREATE FUNCTION ...\n" +
"...\n" +
"...\n" +
"BEGIN\n" +
"\n" +
" DECLARE ...\n" +
" DECLARE ...\n" +
"...\n" +
"END";
Matcher m = Pattern.compile("(?:\\s+DECLARE.+)+").matcher(input);
if (m.find()) {
String firstPart = input.substring(0, m.start());
String secondPart = m.group();
String thirdPart = input.substring(m.end());
System.out.println(thirdPart);
}
1
solved Find RegEx for Pattern [closed]