Graphics Programs Reference
In-Depth Information
second range is the one we're looking for. You can then pass this range in
NSString
's
substringWithRange:
method to get back an
NSString
defined by the range.
In
trimItemTitles
, pull out the range from the
NSTextCheckingResult
, get the
associated string (the post title), and then set it as the item's title.
if ([matches count] > 0) {
NSTextCheckingResult *result = [matches objectAtIndex:0];
NSRange r = [result range];
NSLog(@"Match at {%d, %d} for %@!", 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]];
}
}
Build and run the application. After the connection finishes loading, reveal the master
table view. The rows will now display just the titles of the posts.
You can become more comfortable with regular expressions by playing with
Xcode
's find
interface. Hit Command-F to bring up the
Find Bar
. Then click on the magnifying glass
icon next to the search field and select
Show Find Options
, as shown in
Figure 26.9
.
Change the
Style
pop-up menu to
Regular Expression
.
Figure 26.9 Regular expression search in Xcode
Now you can experiment with applying different regular expressions to your code files.