Hi Cameron,
As far as I can tell, you have an error in your syntax. You're using a -
(hyphen) character in the first match, but Regex uses this to define a
range of characters. It seems that if you don't escape it, the whole
thing behaves a little strangely. For example, using your URL as the
source string:
http://www.domain.com/path1/path2.php?PHPSESSID=2u0ccaffoh6jaeapkke35qpp87;
This regular expression
/^(http:\/\/www\.domain\.com([\/\.\-\_\w]*))/
matches
[!MATCH!]?PHPSESSID=2u0ccaffoh6jaeapkke35qpp87;
and this
/^(http:\/\/www\.domain\.com([\/\.-\_\w]*))/
matches the whole URL
The only difference is the hyphen has been escaped in the first
expression.
I'm not sure if this will help any, but it's definitely something I
noticed in the regex.
Ash
www.ashleysheridan.co.uk
--- Begin Message ---
- To: <php-general@xxxxxxxxxxxxx>
- Subject: Negative Look Ahead Regex - Code Mistake or Bug?
- From: "Cameron B. Prince" <cplists@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 17 Aug 2008 17:55:34 -0500
- Delivery-date: Sun, 17 Aug 2008 23:55:56 +0100
- Envelope-to: ash@xxxxxxxxxxxxxxxxxxxx
- Thread-index: AckAvFsom/qg7AtHQUiTg8E3ny5z1A==
- Thread-topic: Negative Look Ahead Regex - Code Mistake or Bug?
- User-agent: Microsoft-Entourage/12.1.0.080305
Hello,
I¹ve run into a problem with a regex and need help determining if this is my
mistake or a bug. The regex is for inserting a SID into every link in the
buffer before it¹s flushed, but only when each link doesn¹t already have a
SID.
An example of the code is here:
$buffer =
preg_replace('/"http\:\/\/www\.domain\.com([\/\w\.-]*)(?!\?PHPSESSID\=2u0cca
ffoh6jaeapkke35qpp87;?)/',
'"http://www.domain.com$1?PHPSESSID=t9gksvpdcuobsnqt98qloe6lg4;', $buffer);
The code works correctly in most cases except when a SID already exists and
there is a path after the .com such as:
http://www.domain.com/path1/path2.php?PHPSESSID=2u0ccaffoh6jaeapkke35qpp87;
When this URL is processed by the regex, it becomes:
http://www.domain.com/path1/path2.ph?PHPSESSID=t9gksvpdcuobsnqt98qloe6lg4;p?
PHPSESSID=2u0ccaffoh6jaeapkke35qpp87;
So the problem is the regex not only falsely matches, it's inserting the SID
in between 2nd and 3rd letters of the extension.
You can see the code in action here:
http://kottmann.com/test.php
The full source for the test code is also available when viewing the source
of this page.
I've tested this on PHP v5.1.6 and PHP v5.2.6.
TIA,
Cameron
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php