Friday, 8 May 2015

How to get sub strings from a string using Regular Expression

Hi, The below code is to get sub-strings from a string. You are going to love this below code which is very simple but very effective. I have used regular expression to get the sub strings.
  1. NSString *strTest = @"Hii how are you doing @Ravi , how do u do @Kiran where are you @Varun";
  2. NSError *error;
  3. NSRegularExpression *exp =[NSRegularExpression regularExpressionWithPattern:@"@[^ ]*" options:NSRegularExpressionCaseInsensitive error:&error];
  4. NSArray *s1= [exp matchesInString:strTest options:NSMatchingReportCompletion range:NSMakeRange(0, [strTest length]) ];
  5. [s1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  6. NSTextCheckingResult *result = obj;
  7. NSString *str = [strTest substringWithRange:result.range];
  8. NSLog(@"str %@",str);
  9. }];

For such more Blogs you can visit to http://findnerd.com/NerdDigest

No comments:

Post a Comment