Jump to content

Negative lookahead


Drongo_III

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.