Drongo_III Posted August 12, 2014 Share Posted August 12, 2014 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: can you use a negative lookahead in this way? If not, how can I achieve what I'm trying to? Any help appreciated! Drongo Quote Link to comment https://forums.phpfreaks.com/topic/290428-negative-lookahead/ Share on other sites More sharing options...
requinix Posted August 12, 2014 Share Posted August 12, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290428-negative-lookahead/#findComment-1487622 Share on other sites More sharing options...
Drongo_III Posted August 12, 2014 Author Share Posted August 12, 2014 (edited) 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 August 12, 2014 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/290428-negative-lookahead/#findComment-1487624 Share on other sites More sharing options...
requinix Posted August 12, 2014 Share Posted August 12, 2014 Missing a period for the extension. Quote Link to comment https://forums.phpfreaks.com/topic/290428-negative-lookahead/#findComment-1487628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.