By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,716 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,716 IT Pros & Developers. It's quick & easy.

Regular Expression: Backreference Inside Look-ahead

P: n/a
Are backreferences inside look-aheads permitted?

I am trying to remove a name=value pair from a url's query string
using php's preg_replace.

Here's what I am trying to accomplish:

// query string tests
$qs = 'var2=two&var1=one&var3=three'; // start
$qs = 'var1=one&var2=two&var3=three'; // middle
$qs = 'var1=one&var3=three&var2=two'; // end

// re - here's my best guess
preg_replace( '/&(?=(?:\1$))(var2=[^&]*)&?/', '', $qs );

// the output I'd like from preg_replace inputting any of the three $qs
'var1=one&var3=three'

Any help appreciated.
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Clint Pachl wrote:
Are backreferences inside look-aheads permitted?
Yes.
I am trying to remove a name=value pair from a url's query string
using php's preg_replace.

Here's what I am trying to accomplish:

// query string tests
$qs = 'var2=two&var1=one&var3=three'; // start
$qs = 'var1=one&var2=two&var3=three'; // middle
$qs = 'var1=one&var3=three&var2=two'; // end

// re - here's my best guess
preg_replace( '/&(?=(?:\1$))(var2=[^&]*)&?/', '', $qs );
This, I believe, always fails. When the back reference is
reached, its subpattern hasn't yet captured anything. So
you're in effect looking ahead for an empty string that is
at the end [1] of the subject string; but after that you're
matching 'var2=[^&]*)&?'.
// the output I'd like from preg_replace inputting any of the three $qs
'var1=one&var3=three'


You don't need lookaheads:

`(&)?var2=[^&]*(?(1)|&)`

First match and capture an optional ampersand, followed by
'var2=' and zero or more characters excluding ampersands.
Then, if the optional ampersand matched, don't match
anything else; if it didn't, match an ampersand.
[1] 'End' meaning, by default, either the very end or right
before a newline that is the last character.

--
Jock
Jul 17 '05 #2

P: n/a
John Dunlop <us*********@john.dunlop.name> wrote in message news:<MP************************@News.Individual.N ET>...
Clint Pachl wrote:
Are backreferences inside look-aheads permitted?
Yes.
I am trying to remove a name=value pair from a url's query string
using php's preg_replace.

Here's what I am trying to accomplish:

// query string tests
$qs = 'var2=two&var1=one&var3=three'; // start
$qs = 'var1=one&var2=two&var3=three'; // middle
$qs = 'var1=one&var3=three&var2=two'; // end

// re - here's my best guess
preg_replace( '/&(?=(?:\1$))(var2=[^&]*)&?/', '', $qs );


This, I believe, always fails. When the back reference is
reached, its subpattern hasn't yet captured anything. So
you're in effect looking ahead for an empty string that is
at the end [1] of the subject string; but after that you're
matching 'var2=[^&]*)&?'.
// the output I'd like from preg_replace inputting any of the three $qs
'var1=one&var3=three'


You don't need lookaheads:

`(&)?var2=[^&]*(?(1)|&)`


This expression works like a charm.

preg_replace( '/(&)?var2=[^&]*(?(1)|&)/', '', $qs )

First match and capture an optional ampersand, followed by
'var2=' and zero or more characters excluding ampersands.
Then, if the optional ampersand matched, don't match
anything else; if it didn't, match an ampersand.
[1] 'End' meaning, by default, either the very end or right
before a newline that is the last character.


It's nice when the solution is simple and elegant.
Thanks John.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.