PDA

View Full Version : Regular Expression String Matching.




djcraze
Apr 13, 2009, 08:56 PM
Basically I want to run an NSURLConnection and as data is being sent, run a Regular Expression match on it to see if the data contains a websites <title></title> brackets. If it does contain it, use a regular expression match to grab the sites title and put it into an NSString.

I know how to do regular expressions in other languages, and I know the basic syntax.. I just don't know how to apply my knowlege to Objective-C.. :-\. I pretty much hopped from PHP to Objective-C without any prior knowlege of C.

Thanks in advance!
Kyle



djcraze
Apr 13, 2009, 10:35 PM
After fiddling for awhile, I came up with this for anyone who cares.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *stringFromData = [NSString stringWithCString:[theData bytes] length:[theData length]];
stringFromData = [stringFromData lowercaseString];
NSRange beforeTitle = [stringFromData rangeOfString:@"<title>"];
NSRange afterTitle = [stringFromData rangeOfString:@"</title>"];
NSRange myRange = NSMakeRange((beforeTitle.location + (int)7), (afterTitle.location - beforeTitle.location - (int)7));
NSString *afterString = [stringFromData substringWithRange:myRange];
NSLog(@"%@",afterString);
}

kingthong
Dec 4, 2010, 03:49 AM
After fiddling for awhile, I came up with this for anyone who cares.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *stringFromData = [NSString stringWithCString:[theData bytes] length:[theData length]];
stringFromData = [stringFromData lowercaseString];
NSRange beforeTitle = [stringFromData rangeOfString:@"<title>"];
NSRange afterTitle = [stringFromData rangeOfString:@"</title>"];
NSRange myRange = NSMakeRange((beforeTitle.location + (int)7), (afterTitle.location - beforeTitle.location - (int)7));
NSString *afterString = [stringFromData substringWithRange:myRange];
NSLog(@"%@",afterString);
}


Slogged for quite a while before i came across this. million thanks!