Regex can be used to extract portions of a string enclosed in brackets or another set of paired symbols, perhaps to use the contents or to delete the contents.
There are various ways of extracting portions of a string between square brackets. One method involves a wildcard character along with the opening and closing square brackets. Others involve using a look ahead and look behind, or using a negated character set to match everything except a closing square bracket.
The best solution for this is:
/\[(.+?)\]/Edit with Regexity
Let’s break down each of the methods and see how they work.
Method 1: Using a Wildcard Character
We can start building the expression by matching an opening and closing bracket [Edit with Regexity and ]Edit with Regexity. Note that these characters are reserved characters and must therefore be escaped i.e. \[Edit with Regexity and \]Edit with Regexity.
/\[\]/Edit with Regexity
Next, we need can add the wildcard character .Edit with Regexity in between the square braces, which will match any character.
/\[.\]/Edit with Regexity
This will only match a single character between the brackets. To match more characters, we can add a one-or-more +Edit with Regexity quantifier behind the wildcard character, allowing it to match one or more chacaters.
/\[.+\]/Edit with Regexity
If your specific use case can accept empty content (i.e. no characters between the square brackets), you can also use the zero-or-more *Edit with Regexity quantifier.
Note that these quantifiers are greedy by default, matching as many characters as possible that satisfies the criteria. To make the quantifier behave lazy, matching as few characters as possible, add a lazy modifier ?Edit with Regexity behind the quantifier.
/\[.+?\]/Edit with Regexity
Finally, we need to add a capture group around the middle portion to ensure only the contents are extracted and captured in a capture group.
/\[(.+?)\]/Edit with Regexity
Using this expression, the contents of the square brackets will be captured in the first capture group.
Method 2: Using a Negated Character Class
The second method involves using a negated character set to match everything except a closing square bracket. We start the expression by first matching an opening bracket [Edit with Regexity. Remember that this a reserved character in regex and therefore we must escape it:
Next we we will use a character class […]Edit with Regexity which allows us to match any character contained in the class. However, instead of specifying every possible character we might encounter here, we can simply flip the whole thing on its head and use a negated character class [^…]Edit with Regexity to specify the characters we don’t want to match.
We know that once we encounter a closing square bracket ]Edit with Regexity that the match will be complete, so we can say with safety that we’d like to match any character except a closing square bracket.
To negate the character class we use a caret symbol ^Edit with Regexity after the first opening square bracket. So we add to our expression as follows:
/\[[^]]/Edit with Regexity
Don’t get confused here. The first opening square bracket is escaped and matched an opening square bracket literally. This is followed by a full negated character class (of which the first and last characters are square brackets), which contains a literal closing square bracket. Since the closing square bracket is inside a character class there’s no need to escape it here.
However, the expression above will only match one character after the opening square bracket. We can include a one-or-more quantifier +Edit with Regexity after the character class:
/\[[^]]+/Edit with Regexity
And finally, once we don’t want to include the opening square bracket in the contents, so we need to use a capture group around the character class and quantifier:
/\[([^]]+)/Edit with Regexity
Method 3: Using Look-ahead and Look-behind
The third method involves much of the first method’s workings, except that we replace the opening and closing square bracket literal matches with a look-behind and look-ahead.
A positive look-behind is in the form of (?<=…)Edit with Regexity with …Edit with Regexity being the expression we are looking behind for. Using this in our expression will ensure that some character is present before the match, without actually including it in the match.
In this case we are looking for an opening square bracket [Edit with Regexity so we start our expression like:
/(?<=[)/Edit with Regexity
After the look-behind, we include the same middle portion as described in method 1 above, which used a wildcard character to match one or more of any character.
/(?<=[).*?/Edit with Regexity
Next we must use a positive look-ahead to ensure the match is followed by a closing square bracket. A positive look-ahead takes the form (?=…)Edit with Regexity with …Edit with Regexity being the expression to look ahead for, and ensures that the match is followed by a certain character without including it in the match. To ensure the match is followed by a closing square bracket ]Edit with Regexity we add to our expression as follows:
/(?<=[).*?(?=])/Edit with Regexity
There is no need to include a capture group here since the opening and closing square brackets won’t be included in the expression.
The regular expressions on this page were adapted from a solution presented on Stack Overflow, posted by various users on this question.