git.fiddlerwoaroof.com
Raw Blame History
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd">
<html><HEAD>
    <title>API Documentation</title>
	<meta name="generator" content="HeaderDoc">
</HEAD>
<BODY bgcolor="#ffffff">
<H1>AGRegex</H1><hr>
An Perl-compatible regular expression class.
<hr><br>
An AGRegex is created with -initWithPattern: or -initWithPattern:options: or the corresponding class methods +regexWithPattern: or +regexWithPattern:options:. These take a regular expression pattern string and the bitwise OR of zero or more option flags. For example:
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;AGRegex *regex = [[AGRegex alloc] initWithPattern:&#64;"(paran|andr)oid" options:AGRegexCaseInsensitive];</code>
<br><br>
Matching is done with -findInString: or -findInString:range: which look for the first occurrence of the pattern in the target string and return an AGRegexMatch or nil if the pattern was not found.
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;AGRegexMatch *match = [regex findInString:&#64;"paranoid android"];</code>
<br><br>
A match object returns a captured subpattern by -group, -groupAtIndex:, or -groupNamed:, or the range of a captured subpattern by -range, -rangeAtIndex:, or -rangeNamed:. The subpatterns are indexed in order of their opening parentheses, 0 is the entire pattern, 1 is the first capturing subpattern, and so on. -count returns the total number of subpatterns, including the pattern itself. The following prints the result of our last match case:
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; i &lt; [match count]; i++)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSLog(&#64;"%d %&#64; %&#64;", i, NSStringFromRange([match rangeAtIndex:i]), [match groupAtIndex:i]);</code>
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;0 {0, 8} paranoid<br />
&nbsp;&nbsp;&nbsp;&nbsp;1 {0, 5} paran</code>
<br><br>
If any of the subpatterns didn't match, -groupAtIndex: will  return nil, and -rangeAtIndex: will return {NSNotFound, 0}. For example, if we change our original pattern to "(?:(paran)|(andr))oid" we will get the following output:
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;0 {0, 8} paranoid<br />
&nbsp;&nbsp;&nbsp;&nbsp;1 {0, 5} paran<br />
&nbsp;&nbsp;&nbsp;&nbsp;2 {2147483647, 0} (null)</code>
<br><br>
-findAllInString: and -findAllInString:range: return an NSArray of all non-overlapping occurrences of the pattern in the target string. -findEnumeratorInString: and -findEnumeratorInString:range: return an NSEnumerator for all non-overlapping occurrences of the pattern in the target string. For example,
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;NSArray *all = [regex findAllInString:&#64;"paranoid android"];</code>
<br><br>
The first object in the returned array is the match case for "paranoid" and the second object is the match case for "android".
<br><br>
AGRegex provides the methods -replaceWithString:inString: and -replaceWithString:inString:limit: to perform substitution on strings.
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;AGRegex *regex = [AGRegex regexWithPattern:&#64;"remote"];<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSString *result = [regex replaceWithString:&#64;"complete" inString:&#64;"remote control"]; // result is "complete control"</code>
<br><br>
Captured subpatterns can be interpolated into the replacement string using the syntax $x or ${x} where x is the index or name of the subpattern. $0 and $& both refer to the entire pattern. Additionally, the case modifier sequences \U...\E, \L...\E, \u, and \l are allowed in the replacement string. All other escape sequences are handled literally.
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;AGRegex *regex = [AGRegex regexWithPattern:&#64;"[usr]"];<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSString *result = [regex replaceWithString:&#64;"\\u$&amp;." inString:&#64;"Back in the ussr"]; // result is "Back in the U.S.S.R."</code>
<br><br>
Note that you have to escape a backslash to get it into an NSString literal. 
<br><br>
Named subpatterns may also be used in the pattern and replacement strings, like in Python. 
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;AGRegex *regex = [AGRegex regexWithPattern:&#64;"(?P&lt;who&gt;\\w+) is a (?P&lt;what&gt;\\w+)"];<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSString *result = [regex replaceWithString:&#64;"Jackie is a $what, $who is a runt" inString:&#64;"Judy is a punk"]); // result is "Jackie is a punk, Judy is a runt"</code>
<br><br>
Finally, AGRegex provides -splitString: and -splitString:limit: which return an NSArray created by splitting the target string at each occurrence of the pattern. For example:
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;AGRegex *regex = [AGRegex regexWithPattern:&#64;"ea?"];<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSArray *result = [regex splitString:&#64;"Repeater"]; // result is "R", "p", "t", "r"</code>
<br><br>
If there are captured subpatterns, they are returned in the array. 
<br><br>
<code>&nbsp;&nbsp;&nbsp;&nbsp;AGRegex *regex = [AGRegex regexWithPattern:&#64;"e(a)?"];<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSArray *result = [regex splitString:&#64;"Repeater"]; // result is "R", "p", "a", "t", "r"</code>
<br><br>
In Perl, this would return "R", undef, "p", "a", "t", undef, "r". Unfortunately, there is no convenient way to represent this in an NSArray. (NSNull could be used in place of undef, but then all members of the array couldn't be expected to be NSStrings.)
<br><br>
<hr><br><center>(Last Updated 9/12/2003)
<br><font size="-1">HTML documentation generated by <a href="http://www.opensource.apple.com/projects" target="_blank">HeaderDoc</a></font>
</center>
</BODY>
</HTML>