31

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

1
  • I'm actually trying to match something thats either on a new line (or a new html line - could be right after <p> too) that has an @ at the beginning with nothing before it...
    – aalaap
    Dec 8, 2008 at 12:28

6 Answers 6

42

\] 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).

8

Works flawlessly:

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

Output:

abecd
5

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 "\\]"

3

You don't need to escape it: if isolated, a ] is treated as a regular character.
Tested with eregi_replace and 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.

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

1

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

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

@"\["

or something like this;

"\\["
1
  • Sorry, then I can't help you :( Though the RegEx syntax should mostly be kind of similar... Dec 8, 2008 at 19:45
0

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

 '\['

In double quotes, though, it could works well depending on the context, since it changes the way the parameter is 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>

3
  • I don't follow you... I am using double quotes in my code and it isn't working.
    – aalaap
    Dec 8, 2008 at 12:30
  • I would have say the reverse, in single quotes, backslash is a character like the others, except before backslash and single quote, IIRC.
    – PhiLho
    Dec 8, 2008 at 12:38
  • Just tried Bombe's code, works fine with '\]', '\\]', "\]" and "\\]"... Actually, ] doesn't need to be escaped if there is no [ before it.
    – PhiLho
    Dec 8, 2008 at 12:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.