homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Regex pattern with a backslash

 3:06 pm on Sep 28, 2012 (gmt 0)

Say you want to match the string: 'php\php'.

Why does the following match fail?

$s = 'php\php';

if (preg_match("/^(php)\\\\1$/",$s,$m))
echo 'match';
echo 'no match';




 2:11 am on Sep 29, 2012 (gmt 0)

Obvious question first: does your string


occur on a line all by itself?

Second question: shouldn't it be
with just three backslashes? That is, two for the literal \ character and one for the "\1" construct? Otherwise you'd be matching the literal string "php\\1" wouldn't you?

Obligatory disclaimer: I don't speak php. But I speak RegEx. After a fashion.


 11:12 am on Sep 29, 2012 (gmt 0)

Yes, the line contains only the string php\php.

I tried with 3 backslashes also, but again, no match was found.I figured that with 4 backslashes, the first 2 match a literal "\", and then the third backslash escapes the backreference "\1" (which is equal to "php"), so all in all you match "php" plus "\" plus "php".

I can't seem to find the error...


 11:14 am on Sep 29, 2012 (gmt 0)

Surely, for
php\php it's php\\php

 12:54 pm on Sep 29, 2012 (gmt 0)

I think I got it. You need 4 backslashes to match a literal backslash, then you need another backslash to escape "\1", all in all 6 backslashes in a row. Nice :)


 1:13 pm on Sep 29, 2012 (gmt 0)

Glad you cracked it.


 12:17 am on Sep 30, 2012 (gmt 0)

Oh, what fun. In order to end up with one literal backslash and one \1 element you have to start with three backslashes-- the extra being for the literal backslash-- and then to construct a rule that produces this result, you have to double each individual backslash, for a total of six. And if you ever had to nest this rule inside something else, you'd need twelve backslashes in a row.

So I was on the right track; I just didn't go far enough.

Moral: don't try to match strings containing literal backslashes ;)

When I first started doing e-books, I thought a handy temporary marker for page breaks would be something in the form //123\\ Ouch. But I'm stuck with it.


 11:44 am on Sep 30, 2012 (gmt 0)

I'd start with using single quotes around strings instead of double quotes, that would more than half the amount of escapes you need.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved