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


/ Published in: Objective C
Save to your folder(s)

Extract groups from a Regular Expression


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.