0

I've the string looking like this:

word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?|word1-word2-\d{1,2}\w?-\d{1,2}\w?|word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w

I'd like to split this string by '|' everywhere where it doesn't precedes by '('. So result should be:

["word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w"]

I've trying to use negative lookahead \((?!\|) which split the text to on '('.

UPDATE

So I want to achieve not splitting the "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w" on '|' where that character is precedes by '('.

Could someone please help me with this?

rusna
  • 366
  • 6
  • 20

1 Answers1

0

Code:

public static void main(String[] args) {
        String str = "word1-word2-word3-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-word3-(\\d{1,2}\\w?\\d{1,2}|\\d{1,2}\\w?)-\\d{1,2}\\w";
        String[] arrOfStr = str.split("\\|");

        for (String a : arrOfStr)
            System.out.println(a);
    }
Kane01
  • 61
  • 1
  • 1
  • 7
  • This would also split on `|` which is inside parenthesis which OP doesn't want. – Pshemo Oct 02 '19 at 12:03
  • Oh, sorry I just saw the update – Kane01 Oct 02 '19 at 12:04
  • No worries, feel free to correct your answer or if you can't simply delete it (any reputation change - here `-2` caused by each downvote - related to it will also be rolled back after few minutes). – Pshemo Oct 02 '19 at 12:06