Question

I am looking to match specific parts of text which is surrounded in square brackets. Either the text before a colon up to the word boundary/whitespace before or if no colon exists just the text before the end square bracket. For example

[Object testThis:anotherObject]; should match testThis

[Object create]; should match create

[Object create:YES andTest:NO]; should match both create as well as andTest.

I was trying something like the following with a look behind to check for an open square bracket and a look ahead to find the close square bracket but I wasn't able to get it to match anything in between.

(?<=\[) .*:(?=\])
Was it helpful?

Solution

Hmm, maybe something like this:

(?<!:)\b[^: ]+\b(?=:[^\[\]]+\]|\])

regex101 demo

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.

OTHER TIPS

I would go simple. You specify a word boundry or whitespace before : or ].
Why make it so complex, a simple

Edited:

(\w+)\s*[:\]]\s*\w*

should do it.

If you want to expand the definition of the "text", just change the \w to whatever that definition is.

Edit2 - Final answer (until specs change again):

(\w+)\s*(?::\s*[^\[\]\s]*|\])

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top