Graphics Programs Reference
In-Depth Information
The first .* is the name of the subforum, the second is the post title, and the third is the
author's name. These are separated by the literal string :: . (Don't forget the surround-
ing whitespaces!) In RSSChannel.m , change the regular expression in the
trimItemTitles method to have this pattern.
NSRegularExpression *reg =
[[NSRegularExpression alloc] initWithPattern:@"Author"
options:0
error:nil];
NSRegularExpression *reg =
[[NSRegularExpression alloc] initWithPattern:@".* :: .* :: .*"
options:0
error:nil];
Build and run the application. By design, every RSSItem 's full title matches this pattern,
so every title is printed to the console. This is a good start. Next we'll use a capture group
to extract the title of the post (the string in the middle of the two :: ) from the full title.
Using a capture group in a regular expression allows you to access a specific substring of
a match. Basically, when a pattern string has one or more capture groups, each NSTex-
tCheckingResult that represents a match will contain an NSRange structure for
each capture group in the pattern.
Capture groups are formed by putting parentheses around a term in the regular expression
pattern. Thus, to get our post title, we will use the following regular expression:
.* :: (.*) :: .*
Update trimItemTitles in RSSChannel.m to capture the string between the
double colons.
NSRegularExpression *reg =
[[NSRegularExpression alloc] initWithPattern:@".* :: .* :: .*"
options:0
error:nil];
NSRegularExpression *reg =
[[NSRegularExpression alloc] initWithPattern:@".* :: (.*) :: .*"
options:0
error:nil];
Now instead of having one range, each NSTextCheckingResult that is returned will
have an array of ranges. The first range is always the range of the overall match. The next
range is the first capture group, the next is the second capture group, and so on. You can
get the capture group range by sending the message rangeAtIndex: to the NSTex-
tCheckingResult . In our regular expression, there is one capture group, so the
 
Search WWH ::




Custom Search