php 如何使用正则表达式匹配方括号文字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349156/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:28:24  来源:igfitidea点击:

How do I match a square bracket literal using RegEx?

phpregexstring

提问by aalaap

What's the regex to match a square bracket? I'm using \\]in a pattern in eregi_replace, but it doesn't seem to be able to find a ]...

匹配方括号的正则表达式是什么?我正在使用\\]中的模式eregi_replace,但它似乎无法找到]...

回答by Michael Borgwardt

\]is correct, but note that PHP itself ALSO has \as an escape character, so you might have to use \\[(or a different kind of string literal).

\]是正确的,但请注意 PHP 本身也有\作为转义字符,因此您可能必须使用\\[(或不同类型的字符串文字)。

回答by Bombe

Works flawlessly:

完美运行:

<?php
    $hay = "ab]cd";
    echo eregi_replace("\]", "e", $hay);
?>

Output:

输出:

abecd

回答by PhiLho

You don't need to escape it: if isolated, a ] is treated as a regular character.
Tested with eregi_replace and preg_replace.

您不需要对其进行转义:如果被隔离,则将 ] 视为常规字符。
使用 eregi_replace 和 preg_replace 进行测试。

[ is another beast, you have to escape it. Looks like single and double quotes, single or double escape are all treated the same by PHP, for both regex families.

[ 又是一头野兽,你必须躲开它。对于两个正则表达式系列,PHP 对单引号和双引号、单转义或双转义的处理都是相同的。

Perhaps your problem is elsewhere in your expression, you should give it in full.

也许你的问题在你表达的其他地方,你应该完整地给出。

回答by Brad Gilbert

There are two ways of doing this:

有两种方法可以做到这一点:

/ [\]] /x;

/  \]  /x;

While you may consider the latter as the better option, and indeed I would consider using it in simpler regexps. I would consider the former, the better option for larger regexps. Consider the following:

虽然您可能认为后者是更好的选择,但实际上我会考虑在更简单的正则表达式中使用它。我会考虑前者,对于更大的正则表达式来说是更好的选择。考虑以下:

/ (\w*) (  [\d\]] ) /x;

/ (\w*) ( \d | \] ) /x;

In this example, the former is my preferred solution. It does a better job of combining the separate entities, which may each match at the given location. It may also have some speed benefits, depending on implementation.

在这个例子中,前者是我的首选解决方案。它在组合单独的实体方面做得更好,每个实体可能在给定的位置匹配。它也可能有一些速度优势,具体取决于实现。

Note: This is in Perl syntax, partly to ensure proper highlighting.
In PHP you may need to double up on the back-slashes."[\\]]"and "\\]"

注意:这是 Perl 语法,部分是为了确保正确突出显示。
在 PHP 中,您可能需要将反斜杠加倍。"[\\]]""\\]"

回答by VonC

You problem may come from the fact you are using eregi_replace with the first parameter enclosed in simple quotes:

您的问题可能来自您使用 eregi_replace 的事实,其中第一个参数用单引号括起来:

 '\['

In double quotes, though, it could works well depending on the context, since it changes the way the parameteris passed to the function (simple quotes just pass the string without any interpretation, hence the need to double to "\" character).

但是,在双引号中,它可以根据上下文很好地工作,因为它改变了参数传递给函数的方式(简单引号只是传递字符串而没有任何解释,因此需要加倍到“\”字符)。

Here, if "\["is interpreted as an escape character, you still need to double "\".

在这里,如果"\["被解释为转义字符,你仍然需要加倍“\”。

Note: based on your comment, you may try the regex

注意:根据您的评论,您可以尝试使用正则表达式

<\s*(?:br|p)\s*\/?\s*\>\s*\[

in order to detect a [right after a <br>or a <p>

为了检测[a<br>或 a之后的权利<p>

回答by Thomas Hansen

In .Net you escape special characters by adding up a backslash; "\" meaning it would become; "["...

在 .Net 中,您可以通过添加反斜杠来转义特殊字符;“\”意味着它会变成;“[”...

Though since you normally do this in string literalsyou would either have to do something like this;

尽管您通常在字符串文字中执行此操作,因此您必须执行以下操作;

@"\["

or something like this;

或类似的东西;

"\["