Home > other >  Usin Regex, find any text inside square brackets, ignoring the ones with a preceding backlash ("
Usin Regex, find any text inside square brackets, ignoring the ones with a preceding backlash ("

Time:05-21

I'm trying to find a regular expression that will match all groups of text inside square brackets, except the ones with a preceding backlash. To ilustrate my point, given this text:

[#5C9269]\[Go to [size=120%]1[/size] now][/color]

Only the groups #5C9269, size=120%, /size and /color will match, ignoring the group Go to [size=120%]1[/size] now with the preceding backlash character.

My best attempt at this problem was the following regex /([^\\])\[([^{}]*?)\]/, looking for the absence of a preceding backlash and capturing the target text in two groups, but this expression fails to capture valid matches at the start of the line as there's no character before them, like in my previous example.

CodePudding user response:

In your pattern you are matching a character ([^\\]) at the start of the string which is expected to be present.

You can exclude a preceding backslash using a negative lookbehind (which is non consuming). Then in the character class exclude matching [ and ] instead of { and }

Then you can also remove the non greedy quantifier ? from [^{}]*? as the square brackets can not cross the closing one.

The values that you want are in capture group 1.

(?<!\\)\[([^][] )]

Explanation

  • (?<!\\) Positive lookbehind, assert no \ directly to the left of the current position
  • \[ Match the opening
  • ([^][] ) Capture group 1, match 1 occurrences of any char except [ and ]
  • ] Match the closing ]

See a regex demo.

If you also want to match emtpy strings between the square brackets you can use [^][]*

  • Related