Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Foundation has a <a href="http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html" rel="nofollow">NSRegularExpression</a> class (iOS4 and later), which may be useful to you. From the docs: </p> <blockquote> <p>The fundamental matching method for NSRegularExpression is a Block iterator method that allows clients to supply a Block object which will be invoked each time the regular expression matches a portion of the target string. There are additional convenience methods for returning all the matches as an array, the total number of matches, the first match, and the range of the first match.</p> </blockquote> <p>For example:</p> <pre><code>NSString *input = @"Hello, how are you today?"; // make a copy of the input string. we are going to edit this one as we iterate NSMutableString *output = [NSMutableString stringWithString:input]; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\w+" options:NSRegularExpressionCaseInsensitive error:&amp;error]; // keep track of how many additional characters we've added (1 per iteration) __block NSUInteger count = 0; [regex enumerateMatchesInString:input options:0 range:NSMakeRange(0, [input length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ // Note that Blocks in Objective C are basically closures // so they will keep a constant copy of variables that were in scope // when the block was declared // unless you prefix the variable with the __block qualifier // match.range is a C struct // match.range.location is the character offset of the match // match.range.length is the length of the match NSString *matchedword = [input substringWithRange:match.range]; // the matched word with the length appended NSString *new = [matchedword stringByAppendingFormat:@"%d", [matchedword length]]; // every iteration, the output string is getting longer // so we need to adjust the range that we are editing NSRange newrange = NSMakeRange(match.range.location+count, match.range.length); [output replaceCharactersInRange:newrange withString:new]; count++; }]; NSLog(@"%@", output); //output: Hello5, how3 are3 you3 today5? </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload