php 如何使用正则表达式删除方括号和它们之间的任何内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2359921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove square brackets and anything between them with a regex?
提问by Steven Mercatante
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
回答by Tom Mayfield
[and ]are special characters in a regex. They are used to list characters of a match. [a-z]matches any lowercase letter between aand z. [03b]matches a "0", "3", or "b". To match the characters [and ], you have to escape them with a preceding \.
[和]是正则表达式中的特殊字符。它们用于列出匹配的字符。[a-z]匹配a和之间的任何小写字母z。[03b]匹配“0”、“3”或“b”。要匹配字符[和],您必须使用前面的 对它们进行转义\。
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"] hereand turn it into an example here.
贪婪匹配可以匹配多个 [s 和 ]s。那个表达式会把an example [of "sneaky"] text [with more "sneaky"] here它变成an example here.
Perl has a syntax for a non-greedy match (you most likely don't want to be greedy):
Perl 有一个非贪婪匹配的语法(你很可能不想贪婪):
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"] herebecomes an example text here.
非贪婪匹配尝试捕获尽可能少的字符。使用相同的示例:an example [of "sneaky"] text [with more "sneaky"] here变成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.
另请注意,所有这些都可能因格式错误的输入而失败。多个[s 和]s 不匹配可能会导致令人惊讶的结果。
回答by Andres SK
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
这很酷
回答by No Refunds No Returns
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.
我认为您实际上想要外括号的括号,因为它是一个组。方括号是一系列表达式。不知道如何在 SO 中输入它。
/(\[.*\])/

