Graphics Programs Reference
In-Depth Information
Regular expressions are a powerful way of searching text. When you create a regular ex-
pression, you devise a pattern string that represents the pattern you want to find in some
other string. So, the purpose of a regular expression is to find substrings within a larger
string. You apply the regular expression to the searched string, and if the pattern is found,
then the searched string is said to be a match .
In iOS, a regular expression is represented by an instance of NSRegularExpression .
An NSRegularExpression is initialized with a pattern string and can be applied to
any instance of NSString . This is done with the NSRegularExpression instance
method matchesInString:options:range: . You pass the string to be searched,
and it returns an array of matches. Each match is represented by an instance of the class
NSTextCheckingResult . (If there are no matches, then an empty array is returned.)
Every NSTextCheckingResult has a range property that identifies the location and
length of the match in the searched string. The range property is an NSRange structure.
A range has a location (the index of the character where the match begins) and a length
(the number of characters in the match).
Figure 26.8 shows some examples of strings matched to the pattern string “ This is a
pattern ” and the matched range. When a string has more than one match, there are
multiple instances of NSTextCheckingResult in the array returned from
matchesInString:options:range: .
Figure 26.8 Range of matched strings
 
Search WWH ::




Custom Search