Forum Moderators: coopster

Message Too Old, No Replies

trying to match a square bracket in regex

         

darkage

5:43 pm on Dec 9, 2007 (gmt 0)

10+ Year Member



I want to match the following strings (case insensitive):
[IMG X]

(X is a number)

This is what I have so far (will match "img X":
preg_match_all ("/\bimg (\d+)\b/i" , $description, $matches)

How to i modify it so it includes the opening and closing squarebrackets?

Doing it like this DOES NOT work:
preg_match_all ("/\b\[img (\d+)\]\b/i" , $description, $matches)

Thanks in advance.

Receptional Andy

8:55 pm on Dec 9, 2007 (gmt 0)



Maybe I've misunderstood, but in your non-working example you are still searching for word boundaries (\b). Is the output you want with square brackets instead of these?

preg_match_all ("/\[img (\d+)\]/i" , $description, $matches);

darkage

10:22 pm on Dec 9, 2007 (gmt 0)

10+ Year Member



Andy, you're right.

Just reread on word boundaries and square brackets are not a part of them.

Thanks!