Regular Expression String Matching.

Discussion in 'iPhone/iPad Programming' started by djcraze, Apr 13, 2009.

  1. macrumors regular

    Joined:
    Jul 3, 2007
    #1
    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
     
  2. thread starter macrumors regular

    Joined:
    Jul 3, 2007
    #2
    After fiddling for awhile, I came up with this for anyone who cares.
    Code:
    - (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);
    }
    
     
  3. macrumors member

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

Share This Page