Hmm, maybe something like this:
(?<!:)\b[^: ]+\b(?=:[^\[\]]+\]|\])
The regex started as:
[^: ](?=:[^\[\]]+\])
To get testThis
in 1, create
and andTest
in 3. This would get the words before a colon only if they are between square brackets (assuming they are balanced). Then for the second case, I added the negative lookbehind and the or \]
in the lookahead, plus the word boundaries to the match to prevent partial matches.
EDIT: As per comment:
(?<!:)\b[^: ]+\b(?=:[^\]]+\]|\])
Should work with [Object create:YES andTest:[Object anotherTest]]
as well.
EDIT2: Possible alternative:
[^\]]+?(\b[^: ]+\b)[:\]]
But here, you have to fetch the results from the first capture group.