Search code examples
regexregex-lookaroundslookbehind

Match specific text which has square bracket before and after (~ start & end of line)


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.

(?<=\[) .*:(?=\])

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.