Posted By

BenClayton on 02/15/12


Tagged

iphone ios


Versions (?)

iOS / iPhone: Extract groups from a Regular Expression (NSRegularExpression)


 / Published in: Objective C
 

Extract groups from a Regular Expression

  1. NSRegularExpression* exp = [NSRegularExpression regularExpressionWithPattern:@"(put)-(expression)-(here)" options:NSRegularExpressionSearch error:&error];
  2.  
  3. if (error) {
  4. NSLog(@"%@", error);
  5. } else {
  6.  
  7. NSTextCheckingResult* result = [exp firstMatchInString:testString options:0 range:NSMakeRange(0, [productDescription length] ) ];
  8.  
  9. if (result) {
  10.  
  11. NSRange groupOne = [result rangeAtIndex:1]; // 0 is the WHOLE string.
  12. NSRange groupTwo = [result rangeAtIndex:2];
  13. NSRange groupThree = [result rangeAtIndex:3];
  14.  
  15. NSLog( [testString substringWithRange:groupOne] );
  16. NSLog( [testString substringWithRange:groupTwo] );
  17. NSLog( [testString substringWithRange:groupThree] );
  18. }
  19. }

Report this snippet  

You need to login to post a comment.