A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearRegular Expression to Detect a Specific Query - page 1
User InfoPosts
Regular Expression to Detect a Specific Query#1
I wonder if you anyone can construct a regular expression that can detect if a person searches for something like "site:cnn.com" or "site:www.globe.com.ph/". I've been having the most difficult time figuring it out. Thanks a lot in advance!

Edit: Sorry forgot to mention my script is in PHP.

posted date: 2008-12-26 06:48:00


Re: Regular Expression to Detect a Specific Query#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-26 06:48:01


Re: Regular Expression to Detect a Specific Query#3
the request is not specific enough : do you just want cnn.com or www.globe.com.ph OR any site:<site> ?

posted date: 2008-12-26 07:10:00


Re: Regular Expression to Detect a Specific Query#4
What are you matching against? A referer url?Assuming you're matching against a referer url that looks like this:http://www.google.com/search?client=safari&rls=en-us&q=whatever+site:foo.com&ie=UTF-8&oe=UTF-8A regex like this should do the trick:\bsite(?:\:|%3[aA])(?:(?!(?:%20|\+|&|$)).)+Notes:The colon after 'site' can either be unencoded or it can be percent encoded. Most user agents will leave it unencoded (which I believe is actually contrary to the standard), but this will handle bothI assumed the site:... url would be right-bounded by the equivalent of a space character, end of field (&) or end of string ($)I didn't assume x-www-form-urlencoded encoding (spaces == '+') or spaces encoded with percent encoding (space == %20). This will handle bothThe (?:...) is a non-capturing group. (?!...) is a negative lookahead.

posted date: 2008-12-26 07:16:00


Re: Regular Expression to Detect a Specific Query#5
no it's not for a referrer url. My php script basically spits out information about a domain (e.g. backlinks, pagerank etc) and I need that regex so it will know what the user is searching for. If the user enters something that doesn't match the regex, it does a regular web search instead.

posted date: 2008-12-26 07:26:00


Re: Regular Expression to Detect a Specific Query#6
You need to give us examples of exactly what you're trying to match against.

posted date: 2008-12-26 07:37:00


Re: Regular Expression to Detect a Specific Query#7
Oh, it was in my first message. I want the regex to match something like: site:cnn.com or site:globe.com.ph or site:bbc.co.uk If these are matched the script displays the domain information.

posted date: 2008-12-26 07:45:00


Re: Regular Expression to Detect a Specific Query#8
you're saying what you want to match, but what text do you want to match against? arbitrary user input into a text field? something else?

posted date: 2008-12-26 07:56:00


Re: Regular Expression to Detect a Specific Query#9
yes, it will be inputed by a user into a text field. Sorry for not mentioning that earlier.

posted date: 2008-12-26 07:58:00


Re: Regular Expression to Detect a Specific Query#10
I mean yes, arbitrary user input into a text field.

posted date: 2008-12-26 07:59:00


Re: Regular Expression to Detect a Specific Query#11
If this is all you are trying to do, I guess I'd take the more simple approach and just do:$entry = $_REQUEST['q'];$tokens = split(':', trim($entry));if (1 < count($tokens) && strtolower($tokens[0]) == 'site') $site = $tokens[1];

posted date: 2008-12-26 08:00:00


Re: Regular Expression to Detect a Specific Query#12
Ok, for input into an arbitary text field, something as simple as the following will work:\bsite:(\S+)where the parentheses will capture whatever site/domain they're trying to search. It won't verify it as valid, but validating urls/domains is complex and there are many easily googlable regexes for doing that, for instance, there's one here.

posted date: 2008-12-26 08:10:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.