Welcome to WebmasterWorld Guest from 54.161.201.189

Forum Moderators: coopster & jatar k

Regex pattern with a backslash

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

5+ Year Member



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';
else
echo 'no match';


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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Obvious question first: does your string

php\php

occur on a line all by itself?

Second question: shouldn't it be
(php)\\\1
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)

5+ Year Member



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)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Surely, for
php\php
it's
php\\php
12:54 pm on Sep 29, 2012 (gmt 0)

5+ Year Member



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)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Glad you cracked it.
12:17 am on Sep 30, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month