Welcome to WebmasterWorld Guest from 54.167.46.29

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regex pattern with a backslash

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

Junior Member

5+ Year Member

joined:Jan 21, 2009
posts: 69
votes: 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';
else
echo 'no match';


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

Senior Member from US 

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

joined:Apr 9, 2011
posts:12716
votes: 244


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 Sept 29, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 21, 2009
posts: 69
votes: 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 Sept 29, 2012 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


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

Junior Member

5+ Year Member

joined:Jan 21, 2009
posts: 69
votes: 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 Sept 29, 2012 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


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

Senior Member from US 

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

joined:Apr 9, 2011
posts:12716
votes: 244


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 Sept 30, 2012 (gmt 0)

Senior Member

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

joined:Aug 7, 2003
posts:4783
votes: 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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members