0

I have the following code which works:

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-cA-C2][m-oM-O][a-cA-C2]" options:0 error:NULL];
    NSString *str = @"Ana";
    NSTextCheckingResult *match1 = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];

    NSLog(@"check is exist: %@", [str substringWithRange:[match1 rangeAtIndex:0]]);

Here are my questions:

1.Is there a way I can change the NSString with an NSMutableArray and save the NSTextCheckingResult in a NSMutableArray called filterArray?

2.How to highlight the matching values when displaying then in a TextField?

Neha
  • 1,751
  • 14
  • 36
just ME
  • 1,817
  • 6
  • 32
  • 53

1 Answers1

0

If I correctly understand your question, you want to use NSArray of strings, and receive NSArray of matching results for each string.

So, 1:

    NSArray *arrayOfStrings = /* Your array */;

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-cA-C2][m-oM-O][a-cA-C2]" options:0 error:NULL];
    NSMutableArray *filterArray = [NSMutableArray array];
    [arrayOfStrings enumerateObjectsUsingBlock:^(NSString * str, NSUInteger idx, BOOL * stop) {
        NSTextCheckingResult *match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
        if (match)  {
            [filterArray addObject:match];
        }
        else {
            [filterArray addObject:[NSNull null]];
        }

        NSLog(@"String #%i. check is exist: %@",idx, [str substringWithRange:[match rangeAtIndex:0]]);
    }];

2: For highlighting ranges of the string you need to use NSAttributedString. Please, see this question for the answer how:) How do you use NSAttributedString? After you formed attributed string, set it to textfield:

    NSAttributedString * attributedString;
    UITextField *textField;
    [ttextField setAttributedText:attributedString];
Community
  • 1
  • 1
Alexander Tkachenko
  • 3,221
  • 1
  • 33
  • 47