If I have a string like this (from a Wiki-markup) that I need to parse in Java:
this link (is [[ inParen ]] and) (this) one is [[ notInParen ]]
I’d like to use regex to extract the texts inside the [[ ]] but not if they are inside parentheses. For example, in the example above it should return:
notInParen
But ignore:
inParen and this
… since they are inside parentheses. I can find the parentheses and the brackets separately no problem:
.*\(.*?\).* and .*?\[\[(.*?\]\].*
…but can’t figure out how to find the [[ ]], look around for parentheses, and ignore. Thanks!
This is a fine regex
Your desired match will be in group 1
FYI, to make it better perform you can minimize backtracking by replacing the lazy match with a negated character class.
In Java this becomes
Note that group 1 will be empty for the cases the first part of the alternation did match.