Chris Finlayson
Chris Finlayson

Reputation: 371

Java regexp - Double negative lookahead - ignore parenthesis - ignore lines starting with '-'

I need to cover off two scenarios within a string function.

  1. Ignore ';' if in parenthesis
  2. Ignore ';' if line starts with '--'

I have a large string containing multiple SQL statements, which I need to split on ';' except in the above scenarios where there is a commented block, or the semi-colon exists in a function - ironically such as REGEXP()

I have tried to put a second negative lookahead block before or after the existing, but it never seems to work.

String[] queries = queryParam.trim().split(";(?![^()]*\\))"); -- Works for parenthesis

String[] queries = queryParam.trim().split(";(?![^()]*\\))(?!-*\-)"); -- Does not work

String[] queries = queryParam.trim().split(";(?!-*\-)(?![^()]*\\))"); -- Does not work

https://regex101.com/r/Sdx8TC/1

Upvotes: 1

Views: 125

Answers (2)

superkytoz
superkytoz

Reputation: 1279

I came up with this regex:

((?:(?:\-\-[^*]*+(?:[^\-\-][^*]*\-\-)*|(?:[^\-\-]|\-\-\n?)*?\n)|(?:\((?:\(.|[^\(])*\(|\((?:\).|[^\)])*\)|.[^\)\;]*))*?)(;)

The regex ensures the following:

  1. Ignore ';' if in parenthesis
  2. Ignore ';' if line starts with '--'

Link to see the regex in action: https://regex101.com/r/AFnWqD/1/

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163277

Your second requirement is if line starts with '--'. In that case the negative lookahead (?!-*\-) is not going to work because your match starts with ; so that line can not start with -- anymore.

What you might do using your patterns is first check if the string starts with -- using for example matches in combination with this pattern:

^[ \\t]*--.*$

Regex demo

If that matches, then use split with your pattern:

;(?![^()]*\\))

For example:

String[] strings = {
    "   --hello;(hello;hello)hello;",
    "hello;(hello;hello)hello;"
};

for (String s : strings) {
    if (!s.matches("^[ \\t]*--.*$")) {
        System.out.println(Arrays.toString(s.split(";(?![^()]*\\))")));
    }
}

Result:

[hello, (hello;hello)hello]

Java demo

Upvotes: 0

Related Questions