需要regex的帮助来删除方括号和中间的任何内容

时间:2022-05-01 21:45:30

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);

preg_replace(" /[\[()\]]/“,”str美元);

I just end up with hello quote="im sneaky" world

我只是以“你好”结束了我的世界

Thanks

谢谢

3 个解决方案

#1


28  

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

是正则表达式中的特殊字符。它们用于列出匹配的字符。[a-z]匹配“a”和“z”之间的任何小写字母。[03b]匹配a "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"] here and turn it into an example here.

一个贪婪的匹配可以匹配多个[s和]s。这个表达式将在这里举一个“卑鄙的”文本的例子,并把它变成这里的一个例子。


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"] here becomes an example text here.

非贪婪匹配试图捕获尽可能少的字符。使用相同的示例:这里有一个示例文本(带有更多的“sneaky”)。


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会导致令人惊讶的结果。

#2


7  

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

这是很酷的

#3


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.

我认为你需要外括号的括号因为它是一个组。方括号是表达式的范围。不知道怎么把它打出来。

/(\\[.*\\])/

#1


28  

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

是正则表达式中的特殊字符。它们用于列出匹配的字符。[a-z]匹配“a”和“z”之间的任何小写字母。[03b]匹配a "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"] here and turn it into an example here.

一个贪婪的匹配可以匹配多个[s和]s。这个表达式将在这里举一个“卑鄙的”文本的例子,并把它变成这里的一个例子。


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"] here becomes an example text here.

非贪婪匹配试图捕获尽可能少的字符。使用相同的示例:这里有一个示例文本(带有更多的“sneaky”)。


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会导致令人惊讶的结果。

#2


7  

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

这是很酷的

#3


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.

我认为你需要外括号的括号因为它是一个组。方括号是表达式的范围。不知道怎么把它打出来。

/(\\[.*\\])/