To search for words between brackets, I use the following code:
final String str = "([word1]word2 {word3})"; final Matcher m = Pattern.compile("(\\(|\\[|\\{)(.*?)(\\)|\\]|\\})").matcher(str); while (m.find()) { System.out.println(m.group(2)); } The bottom line is that my whole expression is wrapped in parentheses and, as planned, everything should be displayed in the result, but the conclusion is:
[word1 word3
I can not write a regular expression, so that it would produce everything that is between the outermost brackets, and if they are missing, just the words in brackets from the string (that is, word1 and word3). Help me please.
[word}in the simplest case, not to mention nested brackets. - PinkTux