Negate set in combination with negative lookahead

58 Views Asked by At

I have this regex ((\.)|(\),)) (([^\.:]+)): to get values between a . and :
The problems is that I don't want to consider the dots inside a parenthesis. My idea was to add a negative lookahead with something like (?!.*\(,) but no success so far.

Here is an example of text:

Simple text (Example with . inside), The key: Value. Another key: sfdfd.

The text i want to extract is The key and Another key

How could i solve this?

1

There are 1 best solutions below

0
ursa On

If you don't have guaranties on parentheses parity - it would be impossible to solve:

"Simple text ))(Example with (. ((inside), The key: Value. Another key: sfdfd."

However if you have such guaranty, e.g. single level only + parity, then you can solve it in two steps

  • remove parts with parentheses,
  • extract your data
public static void main(String[] args) {
    String s = "Simple text (Example with . inside), The key: Value. Another key: sfdfd.";
    System.out.println(Stream.of(s
        .replaceAll("\\([^()]*\\)", "")
        .split("(?:^|:)[^.,]*[.,]\\s*")
    ).filter(it -> !it.isEmpty()).collect(Collectors.toList()));
}

Stdout:

[The key, Another key]