3

I'm working on a string pattern match algorithm. I use NSRegularExpression for finding the matches. For ex: I've to find all words starting with '#' in a string.. Currently I use the following regex function:

static NSRegularExpression *_searchTagRegularExpression;
static inline NSRegularExpression * SearchTagRegularExpression() 
{
     if (!_searchTagRegularExpression) 
     {
          _searchTagRegularExpression = [[NSRegularExpression alloc] 
                                          initWithPattern:@"(?<!\\w)#([\\w\\._-]+)?"
                                                  options:NSRegularExpressionCaseInsensitive 
                                                    error:nil];
     }

     return _searchTagRegularExpression;
}

and I use it as below:

NSString *searchString = @"Hi, #Hash1 #Hash2 #Hash3...";
NSRange searchStringRange = NSMakeRange(0, searchString.length);
NSRegularExpression *regexp = SearchTagRegularExpression();
[regexp enumerateMatchesInString:searchString 
                         options:0 
                           range:searchStringRange 
                      usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{       
  // comes here for every match with range ( in this case thrice )
}];

This works properly. But i just want to know if this is the best way. suggest if there's any better alternative...

manileo86
  • 153
  • 7

2 Answers2

1

Actually your proposed pattern and implementation is quite good:

  • The pattern is quite precise with its use of the (fancy) zero-width negative look behind assertion to make sure you only match at the beginning of a word. It works correctly at the beginning of a string, for example.

  • The implementation reuses the regex object and avoids recompilation of the pattern.

If you wanted me to be nitpicking: You could drop the NSRegularExpressionCaseInsensitive option as your pattern does not use any parts that have a case.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • Thnks for the input.. i should try removing caseinsensitive search and log the time to see the difference !!! – manileo86 Apr 03 '12 at 11:44
-1

What you do is a good way for sure. You can do this too,

for(NSString *match in [string componentSeperatedByString:@" "])
    {
      if([match hasPrefix:@"#"])
       {
         //do what you like.
        }
     }
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • No. That would match any character as a word character (including punctuation and such). It would also match single # characters. – Nikolai Ruhe Apr 03 '12 at 11:17