RE: [PHP] Backslashing the [ and ] in a regex
| From: | Chris W. Parker | Date: | Wed, 04 Feb 2004 22:14:58 +0000 |
| Subject: | RE: [PHP] Backslashing the [ and ] in a regex | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-176845@lists.php.net to get a copy of this message | ||
Sam Masiello <mailto:smasiello@mxlogic.com>
on Wednesday, February 04, 2004 1:36 PM said:
> Thank you for the reply, Adam, but unfortunately it didn't work.
So you want to accept input that may or may not look like the following?
Here is some \[text\] for j00.
Well I'm no regex expert but I *think* you need to do \\\[ and \\\].
[ is a special character in regex so it must be escaped (oh btw, it's
called escaping and not backlashing...) like so: \[. but that's only if
you want to grab a single [ by itself. if you're looking for a \ you'll
also need to escape this since it too is a special character. so you'd
do \\. but in your case you just happen to have everything next to each
other and i think that may be what's confusing you.
\\ will match \
\[ will match [
\\\[ will match \[
(keep in mind that \\ and \[ don't need to be next to each other to
match \[ if they are contained within one of those regex OR lists...
[...])
i used the regex coach (something i highly recommend you get) to
"figure" this out (assuming i'm right at all).
hope this helps!
chris.
p.s. the following regex:
[\w\s\\\[\]\.]*
will match:
Here is some text \[for\] j00.
keep in mind this may or may not be what you want...