Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove square brackets and anything between them with a regex?

Tags:

regex

php

How can I remove text from between square brackets and the brackets themselves?

For example, I need:

hello [quote="im sneaky"] world 

to become:

hello world 

Here's what I'm trying to use, but it's not doing the trick:

preg_replace("/[\[(.)\]]/", '', $str); 

I just ended up with:

hello quote="im sneaky" world 
like image 301
Steven Mercatante Avatar asked Mar 01 '10 23:03

Steven Mercatante


People also ask

How do you escape square brackets in regex?

If you want to remove the [ or the ] , use the expression: "\\[|\\]" . The two backslashes escape the square bracket and the pipe is an "or".

What's the difference between () and [] in regular expression?

In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.

What do the [] brackets mean in regular expressions?

Square brackets ( “[ ]” ): Any expression within square brackets [ ] is a character set; if any one of the characters matches the search string, the regex will pass the test return true.

How do you remove square brackets from string?

replaceAll() to replace square brackets in a String To replace any character from a String, we generally use replaceAll() method of String.


2 Answers

[ and ] are special characters in a regex. They are used to list characters of a match. [a-z] matches any lowercase letter between a and z. [03b] matches a "0", "3", or "b". To match the characters [ and ], you have to escape them with a preceding \.

Your code currently says "replace any character of [](). with an empty string" (reordered from the order in which you typed them for clarity).


Greedy match:

preg_replace('/\[.*\]/', '', $str); // Replace from one [ to the last ] 

A greedy match could match multiple [s and ]s. That expression would take an example [of "sneaky"] text [with more "sneaky"] here and turn it into an example here.

Perl has a syntax for a non-greedy match (you most likely don't want to be greedy):

preg_replace('/\[.*?\]/', '', $str); 

Non-greedy matches try to catch as few characters as possible. Using the same example: an example [of "sneaky"] text [with more "sneaky"] here becomes an example text here.


Only up to the first following ]:

preg_replace('/\[[^\]]*\]/', '', $str); // Find a [, look for non-] characters, and then a ] 

This is more explicit, but harder to read. Using the same example text, you'd get the output of the non-greedy expression.


Note that none of these deal explicitly with white space. The spaces on either side of [ and ] will remain.

Also note that all of these can fail for malformed input. Multiple [s and ]s without matches could cause a surprising result.

like image 54
Tom Mayfield Avatar answered Oct 14 '22 01:10

Tom Mayfield


Just in case you are looking for a recursive removal:

$str = preg_replace("/\[([^\[\]]++|(?R))*+\]/", "", $str); 

That will convert this:

This [text [more text]] is cool

to this:

This is cool

like image 38
Andres SK Avatar answered Oct 14 '22 01:10

Andres SK