Regular Expression String Matching.

Discussion in 'iOS Programming' started by djcraze, Apr 13, 2009.

  1. djcraze macrumors regular


    Jul 3, 2007
    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!
  2. djcraze thread starter macrumors regular


    Jul 3, 2007
    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];
  3. kingthong macrumors member

    Sep 20, 2010
    Somewhere but not here.
    Slogged for quite a while before i came across this. million thanks!

Share This Page