-5

I am used to write regular expression and do understand it however I tried hard to generate regex for below pattern, but finally need help from you guys.

CREATE FUNCTION ...
...
...
BEGIN

  DECLARE ...
  DECLARE ...
...
END

What I am looking for is to extract 3 groups using Java RegEx:

1st section is from CREATE FUNCITON to BEGIN,

2nd is statements starting with DECLARE and

3rd one is rest everything after DECLARE statements to END.

(See below)

---------------------
CREATE FUNCTION ...
...
...
BEGIN
---------------------
  DECLARE ...
  DECLARE ...
---------------------
...
END
---------------------
Pratik
  • 908
  • 2
  • 11
  • 34

1 Answers1

1

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);
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • @Sweepwe, Now a days its a trend to downvote questions on stackoverflow very easily, even if you try to explain it very decently. I did spend so many ours behind this but may be I was not very good with backreferencing and lookahead operators. But I really appreciate your answer and the efforts you put in for me. Thank you very much again :) – Pratik Jun 13 '19 at 07:10