Jump to content

Recommended Posts

Hi Guys

 

Bit stuck on a negative lookahead and not sure that what i want to achieve is possible.

 

Lets says I have a domain: www.mysite.com

 

I want to have a regex that will match www.mysite.com?q=123&g=34 but I don't want to match any file extensions like .php or .html

 

So the first part of matching the query string and the literal url is fairly straightforward (below) which matches any query string that may be applied

/^www.mysite.com[\/]?[\?\=\&a-z0-9]*$/

But then I want to use a negative lookahead to avoid matching a file extension, e.g.

/^www.mysite.com[\/]?[\?\=\&a-z0-9](?!php|html|htm)$/

But this doesn't work - presumably because the negative lookahead is following a character class. 

 

So my questions:

 

  1. can you use a negative lookahead in this way?
  2. If not, how can I achieve what I'm trying to?

 

Any help appreciated! 

 

Drongo

 

 

Link to comment
https://forums.phpfreaks.com/topic/290428-negative-lookahead/
Share on other sites

You're using the lookahead correctly - it's your regular expression that's wrong. You're currently saying that the query string consists of one single character from that set and that is not followed by any of those three extensions.

 

The extension comes before the query string, right? So what you want is a negative lookbehind and in a different location. Which can't be variable length so you actually need two (maybe three) of them.

Link to comment
https://forums.phpfreaks.com/topic/290428-negative-lookahead/#findComment-1487622
Share on other sites

Thanks for the response hopeless.

 

I think maybe I was looking at it incorrectly...

 

So would something like this be correct?

/^www.mysite.com[\/]?[a-z\-\_]*(?!php|html|htm)[\?\=\&a-z0-9]*$/

So match the literal url, an optional forward slash, then match anything that might be a file name (but this is optional), then negative match any of those file extensions and if they aren't present match something representing a query string (optionally).  Does that make sense?

Edited by Drongo_III
Link to comment
https://forums.phpfreaks.com/topic/290428-negative-lookahead/#findComment-1487624
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.