28

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
double-beep
  • 5,031
  • 17
  • 33
  • 41
Steven Mercatante
  • 24,757
  • 9
  • 65
  • 109

3 Answers3

44

[ 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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Tom Mayfield
  • 6,235
  • 2
  • 32
  • 43
  • Hmm, the malformed input is something I may run into. Is it possible to handle the instance of not having a matching ] ? – Steven Mercatante Mar 02 '10 at 00:31
  • I'd have to know how you want it handled. What would you hope for it to do with malformed input? – Tom Mayfield Mar 02 '10 at 16:57
  • As a side note, if you're implementing a custom markup language, there are some really good complete alternatives that are much easier to drop in than doing it on your own from scratch. – Tom Mayfield Mar 02 '10 at 16:58
  • For C#, this worked for me: https://stackoverflow.com/a/44314170/3873799 – alelom Sep 30 '21 at 09:45
13

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

Andres SK
  • 10,779
  • 25
  • 90
  • 152
1

I think you actually want parens for your outer brackets since it's a group. square brackets are a range of expressions. Not sure how to type it in SO.

/(\\[.*\\])/
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43