Forum Moderators: coopster

Message Too Old, No Replies

Regex pattern with a backslash

         

Mister_L

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

10+ 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.

lucy24

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

WebmasterWorld Senior Member 10+ Year Member 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.

Mister_L

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

10+ 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...

g1smd

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

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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

Mister_L

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

10+ 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 :)

g1smd

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

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you cracked it.

lucy24

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

WebmasterWorld Senior Member 10+ Year Member 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.

swa66

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

WebmasterWorld Senior Member 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.