Broken Book Code


#1

I ran into an interesting problem that I’ve been trying to fix in relations to the book code. I did download the original code from the website and can confirm that it is faulty in certain scenarios. I’m not really sure how to fix it, as I’ve been trying with no success. Essentially, in the trimItemTitles code, there are scenerios where this site will send it RSS feeds that it will reject (particularly ones that are too long and or do not have the third ::. The examples I can provide are as follows:




Note that these particular instances did not receive a match when the search ran. Here is my code (ignore the Re filter code as the result is essentially the same with or without it):

[code]-(void)trimItemTitles{
//Create a regular expression with the pattern: Author
//NSRegularExpression reg = [[NSRegularExpression alloc] initWithPattern:@“Author” options:0 error:nil];
NSRegularExpression reg = [[NSRegularExpression alloc] initWithPattern:@". :: (?:Re: )?(.
) :: ." options:0 error:nil]; //The () creates a capture group and an array of ranges for reg @". :: (?:Re: )?(.) :: ."

//Loop through every title of the items in channel
for (RSSItem *i in items) {
    NSString *itemTitle = [i title];
    
   // NSLog(@"\nThe original title: %@\n", [i title]);
    
    //Find matches in the title string. The range argument specifies how much of the title to search; in this case, all of it.
    NSArray *matches = [reg matchesInString:itemTitle options:0 range:NSMakeRange(0, [itemTitle length])];
    
    //If there was a match...
    if ([matches count] > 0) {
         //Print the location of the match in the string and the string
        NSTextCheckingResult *result = [matches objectAtIndex:0];
        
        NSRange r = [result range];
        
        NSLog(@"\nMatch at {%d, %d} for %@!\n", r.location, r.length, itemTitle);
        
        //One capture group, so two ranges, let's verify
        if ([result numberOfRanges] == 2) {
            //Pull out the 2nd range, which will be the capture group
            NSRange r = [result rangeAtIndex:1];
            
            //Set the title of the item to the string within the capture group
            [i setTitle:[itemTitle substringWithRange:r]];
            NSLog(@"new title: %@", [itemTitle substringWithRange:r]);
        }
    }
}

}
[/code]

Here is the console output. Highlighted in red consists of the problematic feed. As you can see, no match is detected and there is no new output. I’ve attempted altering the match code to not ‘require’ the :: more so with code such as ?[::] with no true success. The closest I have gotten allows everything but these lines of code to output with one : at the end. Does anyone have any thoughts?

[quote]2012-10-17 22:53:15.501 Nerdfeed[1586:c07]
The original title: Big Nerd Ranch General Discussions :: Trouble in Memory Management :: Author manish
2012-10-17 22:53:15.502 Nerdfeed[1586:c07]
Match at {0, 83} for Big Nerd Ranch General Discussions :: Trouble in Memory Management :: Author manish!
2012-10-17 22:53:15.502 Nerdfeed[1586:c07] new title: Trouble in Memory Management
2012-10-17 22:53:15.503 Nerdfeed[1586:c07]
The original title: Big Nerd Ranch General Discussions :: Re: Trouble in Memory Management :: Reply by ibex10
2012-10-17 22:53:15.503 Nerdfeed[1586:c07]
Match at {0, 89} for Big Nerd Ranch General Discussions :: Re: Trouble in Memory Management :: Reply by ibex10!
2012-10-17 22:53:15.503 Nerdfeed[1586:c07] new title: Trouble in Memory Management
2012-10-17 22:53:15.503 Nerdfeed[1586:c07]
The original title: Big Nerd Ranch General Discussions :: Re: Storyboard and Orientation :: Reply by MikeyWard
2012-10-17 22:53:15.504 Nerdfeed[1586:c07]
Match at {0, 90} for Big Nerd Ranch General Discussions :: Re: Storyboard and Orientation :: Reply by MikeyWard!
2012-10-17 22:53:15.504 Nerdfeed[1586:c07] new title: Storyboard and Orientation
2012-10-17 22:53:15.505 Nerdfeed[1586:c07]
The original title: 35. The End :: A test post of hope. :: Author skibum
2012-10-17 22:53:15.505 Nerdfeed[1586:c07]
Match at {0, 52} for 35. The End :: A test post of hope. :: Author skibum!
2012-10-17 22:53:15.505 Nerdfeed[1586:c07] new title: A test post of hope.
2012-10-17 22:53:15.505 Nerdfeed[1586:c07]
The original title: 7. View Controllers :: Help with a little extra on the Silver Challenge :: Author oochr1soo
2012-10-17 22:53:15.505 Nerdfeed[1586:c07]
Match at {0, 91} for 7. View Controllers :: Help with a little extra on the Silver Challenge :: Author oochr1soo!
2012-10-17 22:53:15.506 Nerdfeed[1586:c07] new title: Help with a little extra on the Silver Challenge
2012-10-17 22:53:15.506 Nerdfeed[1586:c07]
The original title: 10. Editing UITableView :: Re: HeaderView not showing up :: Reply by tlekan
2012-10-17 22:53:15.506 Nerdfeed[1586:c07]
Match at {0, 75} for 10. Editing UITableView :: Re: HeaderView not showing up :: Reply by tlekan!
2012-10-17 22:53:15.506 Nerdfeed[1586:c07] new title: HeaderView not showing up
2012-10-17 22:53:15.507 Nerdfeed[1586:c07]
The original title: 10. Editing UITableView :: Re: HeaderView not showing up :: Reply by tlekan
2012-10-17 22:53:15.507 Nerdfeed[1586:c07]
Match at {0, 75} for 10. Editing UITableView :: Re: HeaderView not showing up :: Reply by tlekan!
2012-10-17 22:53:15.507 Nerdfeed[1586:c07] new title: HeaderView not showing up
2012-10-17 22:53:15.508 Nerdfeed[1586:c07]
The original title: 13. UIPopoverController and Modal View Controllers :: Exception on iPad running 5.1 :: Author RonTor
2012-10-17 22:53:15.508 Nerdfeed[1586:c07]
Match at {0, 100} for 13. UIPopoverController and Modal View Controllers :: Exception on iPad running 5.1 :: Author RonTor!
2012-10-17 22:53:15.508 Nerdfeed[1586:c07] new title: Exception on iPad running 5.1
2012-10-17 22:53:15.508 Nerdfeed[1586:c07]
[color=#FF0000]The original title: 13. UIPopoverController and Modal View Controllers :: Re: Exception on iPad running 5. …
2012-10-17 22:53:15.509 Nerdfeed[1586:c07]
The original title: 14. Saving, Loading, and Application States :: initWithCoder: shouldn’t it just call the …
2012-10-17 22:53:15.510 Nerdfeed[1586:c07]
The original title: 14. Saving, Loading, and Application States :: Re: initWithCoder: shouldn’t it just call the …[/color]
2012-10-17 22:53:15.511 Nerdfeed[1586:c07]
The original title: 25. Web Services and UIWebView :: RSS Feed in TableView :: Author raginggoat
2012-10-17 22:53:15.511 Nerdfeed[1586:c07]
Match at {0, 76} for 25. Web Services and UIWebView :: RSS Feed in TableView :: Author raginggoat!
2012-10-17 22:53:15.511 Nerdfeed[1586:c07] new title: RSS Feed in TableView
2012-10-17 22:53:15.512 Nerdfeed[1586:c07]
The original title: 25. Web Services and UIWebView :: Re: RSS Feed in TableView :: Reply by raginggoat
2012-10-17 22:53:15.512 Nerdfeed[1586:c07]
Match at {0, 82} for 25. Web Services and UIWebView :: Re: RSS Feed in TableView :: Reply by raginggoat!
2012-10-17 22:53:15.512 Nerdfeed[1586:c07] new title: RSS Feed in TableView
2012-10-17 22:53:15.512 Nerdfeed[1586:c07]
The original title: 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum
2012-10-17 22:53:15.512 Nerdfeed[1586:c07]
Match at {0, 75} for 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum!
2012-10-17 22:53:15.513 Nerdfeed[1586:c07] new title: Test Topic for NerdFeed Caching
2012-10-17 22:53:15.513 Nerdfeed[1586:c07]
The original title: 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum
2012-10-17 22:53:15.513 Nerdfeed[1586:c07]
Match at {0, 75} for 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum!
2012-10-17 22:53:15.513 Nerdfeed[1586:c07] new title: Test Topic for NerdFeed Caching
2012-10-17 22:53:15.513 Nerdfeed[1586:c07]
The original title: 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum
2012-10-17 22:53:15.515 Nerdfeed[1586:c07]
Match at {0, 75} for 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum!
2012-10-17 22:53:15.515 Nerdfeed[1586:c07] new title: Test Topic for NerdFeed Caching
2012-10-17 22:53:15.515 Nerdfeed[1586:c07]
The original title: 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum
2012-10-17 22:53:15.516 Nerdfeed[1586:c07]
Match at {0, 75} for 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum!
2012-10-17 22:53:15.516 Nerdfeed[1586:c07] new title: Test Topic for NerdFeed Caching
2012-10-17 22:53:15.516 Nerdfeed[1586:c07]
The original title: 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum
2012-10-17 22:53:15.517 Nerdfeed[1586:c07]
Match at {0, 75} for 29. Advanced MVCS :: Re: Test Topic for NerdFeed Caching :: Reply by skibum!
2012-10-17 22:53:15.517 Nerdfeed[1586:c07] new title: Test Topic for NerdFeed Caching
2012-10-17 22:53:15.517 Nerdfeed[1586:c07]
The original title: 3. Blocks :: Re: Blocks and ARC :: Reply by janineanne
2012-10-17 22:53:15.518 Nerdfeed[1586:c07]
Match at {0, 54} for 3. Blocks :: Re: Blocks and ARC :: Reply by janineanne!
2012-10-17 22:53:15.518 Nerdfeed[1586:c07] new title: Blocks and ARC
2012-10-17 22:53:15.518 Nerdfeed[1586:c07]
The original title: 19. Object Instance Variables :: When to use [self] :: Author YOOOEE
2012-10-17 22:53:15.519 Nerdfeed[1586:c07]
Match at {0, 68} for 19. Object Instance Variables :: When to use [self] :: Author YOOOEE!
2012-10-17 22:53:15.519 Nerdfeed[1586:c07] new title: When to use [self]
[/quote]


#2

Yeah, looks like the RSS feed lops off the end of the title before it gets to the second set of double-colons. Thats a bummer. Will have to think about it.


#3

I know you want to introduce the reader to RegEx, but isn’t it a little overkill for this task? Why not just make an array from [itemTitle componentsSeparatedByString:@"::"] and get the second element?

- (void)trimItemTitles {
    // Get the second x from a string in the form x::x::x
    for (RSSItem *i in items) {
        NSArray *titleParts = [i.title componentsSeparatedByString:@"::"];
        if (titleParts && titleParts.count > 1) {
            i.title = [titleParts objectAtIndex:1];
        }
    }
}

I don’t know if I have to check titleParts for nil before checking its count, just being safe.

Thanks for a great book!


#4

[quote=“muz1kt3ch”]I know you want to introduce the reader to RegEx, but isn’t it a little overkill for this task? Why not just make an array from [itemTitle componentsSeparatedByString:@"::"] and get the second element?

- (void)trimItemTitles {
    // Get the second x from a string in the form x::x::x
    for (RSSItem *i in items) {
        NSArray *titleParts = [i.title componentsSeparatedByString:@"::"];
        if (titleParts && titleParts.count > 1) {
            i.title = [titleParts objectAtIndex:1];
        }
    }
}

I don’t know if I have to check titleParts for nil before checking its count, just being safe.

Thanks for a great book![/quote]

Considering it took Apple until 2011 to introduce a Regex class for Obj-C, it’s nice to know of its existence.