Ac128
Ac128

Reputation: 23

Replace text with multiple Regular Expressions on iOS

I'm pretty new to using regex in iOS, and I'm experimenting with it. I'm struggling to find a solution to the problem I have. I apologize in advance if this has been answered before or I'm just missing something really obvious and/or using the wrong tool to accomplish my goal.

I have a string that the user will enter in a textfield. How do I then change that to correct a spelling of a word that is contained inside the string?

NSString *userstring = @"Jon Travalta";
NSError *error = NULL;
NSString *modstring = @"";

NSRegularExpression *regex = [[NSRegularExpression alloc]init];
regex = [NSRegularExpression regularExpressionWithPattern:@"(Jon|Johnathan|Jonathen|John)\\s(Travalta|Travalte|Travelta|Travolta)" options:NSRegularExpressionCaseInsensitive error:&error];

modstring = [regex stringByReplacingMatchesInString:userstring options:0 range:NSMakeRange(0,[userstring length]) withTemplate:@"John Travolta"];

That works and corrects the string fine, but let's say the string is something else. I want to keep that regex active to check the string if it contains any of those elements to correct it.

So let's say the string is set, before the above check, to "Anthony Hopkin". I would then also use another regexwithpattern to check the string to correct that one with the correct spelling of the name.

regex = [NSRegularExpression regularExpressionWithPattern:@"(Anthony|Antony|Tony|)\\s(Hopkin|Hapkin|Hopkins)" options:NSRegularExpressionCaseInsensitive error:&error];

modstring = [regex stringByReplacingMatchesInString:userstring options:0 range:NSMakeRange(0,[userstring length]) withTemplate:@"Anthony Hopkins"];

This will replace the string with Anthony Hopkins, even if it's the first string.

If I do a different alloc of NSRegularExpression and different stringByReplacingMatchesInString, it just doesn't do anything and the string stays what it was to begin with.

Upvotes: 1

Views: 377

Answers (2)

technomorph
technomorph

Reputation: 1

I ran into the same thing and I built a method that will build the expression for me from an array of stings.

NSArray* itemsArray = [@“Jon”, @“Johnathan”, @“Johnathen”, @“John”];

NSMutableString* builtString = @“”;

for (NSString* aItem in itemsArray) {
    NSString* formatString;
    if (![builtSting hasSuffix:@“| “] ) {
        formatString = [NSSting stringWithFormat:@“%@”, aItem];
    }
    else {
        formatString = [NSSting stringWithFormat:@“ | %@”, aItem];
    }
[builtSting stringByAppendingString:formatString];
}

———————————-

add any prefixes or suffixes to complete the expression

repeat for last names group

combine the groups and add any additional regex prefixes Or suffixes.

It may be easier to just execute the search on each name, rather than combining a super large multi first names / last names one. (And easier to debug)

Upvotes: 0

Niels Castle
Niels Castle

Reputation: 8069

Regular expressions are not the right tool for spell checking.

In your question you want to make multiple replacements based on regular expressions. For clarity I'd just iterate though your list of possible corrections applying them one at a time.

-(NSString*) naiiveSpellCheckString:(NSString*) s {

  NSDictionary* corrections = @{
                                @"Travolta": @"Travalta|Travalte|Travelta|Travolta",
                                @"John": @"Jon|Johnathan|Jonathen",
                                @"Anthony":@"Anthoni|Antony|Tony",
                                @"Hopkins": @"Hopkin|Hapkin",
                                };

  for (NSString* key in corrections.keyEnumerator) {

      NSError* error;
      NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:[corrections objectForKey:key]
                                    options:NSRegularExpressionCaseInsensitive
                                    error:&error];

      NSAssert1(!error, @"Error parsing regex %@", [corrections objectForKey:key]);

      s = [regex stringByReplacingMatchesInString:s
                                          options:0
                                            range:NSMakeRange(0, s.length) withTemplate:key];

  }

  return s;
}

Upvotes: 2

Related Questions