Web Search & Regular Expression Example
Published Mar 07, 2009
Updated Feb 11, 2013
This code will search the Snippets
section of Hawkee.com and return a list of matching snippets on your iPhone. It demonstrates how to retrieve data from a URL and parse it using a regular expression.
This expands on a very basic Cocoa Touch example by Matt Long:
First complete his instructions. He's got a video that makes the process very clear.
After you've completed his tutorial you need to go to Basic_iPhone_AppAppDelegate.h and add a UIWebView for the search results:
IBOutlet UIWebView *web;
Feel free to remove the Label as it isn't necessary for this example.
Next you need to return to the Interface Builder by double clicking MainWindow.xib under Resources. Here you can delete the Label and drag a UIWebView into place as seen here:
Be sure to connect it to your "web" IBOutlet with a control click as you did in the original tutorial.
Next you'll need to download and include the RegexKitLite
library. Add RegexKitLite.h and RegexKitLite.m to your Classes folder and follow these steps to add a linker flag to make the library work properly:
Project > Edit Active Target
Select the "Build" tab
Find "Other Linker Flags"
Add "-licucore" to the value column
Finally use the following snippet to replace the "click" IBAction you created in the example code:
// Appends the textField search query to the search URL and gets the resulting HTML
NSString *searchTerm = [textField text];
NSMutableString *hawkeeURL = [[NSMutableString alloc] initWithString:@"http://www.hawkee.com/snippets/?search_query="];
NSURL *url = [[NSURL alloc] initWithString:hawkeeURL];
NSString *result = [[NSString alloc] initWithContentsOfURL:url];
// This will contain the HTML we build from parsing the resulting HTML
NSMutableString *page = [[NSMutableString alloc] initWithString:@"<span style='font-size: 30px;'>"];
// Regular expression to match the link to the snippet
NSString *regexString = @"<a href='(.+)' class='large'>(.+)</a>";
// Set these to empty values at first
NSRange matchedRange = NSMakeRange(NSNotFound, 0);
NSError *error = NULL;
// Determine the length of the HTML and search within that range
int length=[result length];
NSRange searchRange = NSMakeRange(0, length);
// done is true when the matchedRange it beyond the length of the result
Boolean done = false;
matchedRange = [result rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:1 error:&error];
// Make sure we found a match within the length of the document
if(NSMaxRange(matchedRange) <= length)
// Grab the first match which is the URL
NSString *snippetURL = [result substringWithRange:matchedRange];
// Grab the second match which is the snippet name
matchedRange = [result rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2 error:&error];
NSString *snippetName = [result substringWithRange:matchedRange];
// Build a link to append to the resulting HTML page.
[page appendString:@"<a href='http://www.hawkee.com"];
// Now start a new range after the previous match that goes to the length of the page.
searchRange = NSMakeRange(NSMaxRange(matchedRange), length - NSMaxRange(matchedRange));
else done = true;
[web loadHTMLString:page baseURL:nil];