简体   繁体   中英

How can I leverage alphabetical order when searching a large string with regex?

I'm making an iPhone word game. I have a text file containing every English word separated by newlines.

I've tried several methods for checking valid words. Using an SQLite database loads just about instantly, but the search time is too long. I can load everything into an array on game load, but that takes around 7 seconds on a two-year-old iPod. Although search is fast then. I found that the best balance is to load the entire file into an NSString and perform a regex:

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"\n%@\n", word] options:NSRegularExpressionCaseInsensitive error:&error];

NSUInteger numberOfMatches = [regex numberOfMatchesInString:self.words options:0 range:NSMakeRange(0, [self.words length])];

NSLog(@"%i matches found", numberOfMatches);

However, this doesn't take advantage of the fact that the list is in alphabetical order. How can I make this faster by taking advantage of that fact?

One twist is that I allow people to use wild characters, so the regex might look something like: @"wo.d"

How about a two level approach, also called indexing?

Check the very first letter and load all words with that very letter into your array. If the loading time for all words is 7 seconds, for words with a certain start letter it should be 7s/24≈0.3s in average.

If you don't have the first letter, you can use the last, second, etc. as well. To support this, you can create indexes in SQLite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM