Last active
December 18, 2015 05:49
-
-
Save chen-hongzhi/5735566 to your computer and use it in GitHub Desktop.
Convert wildcard string to regular expression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NSString *CHRegexPatternForWildcardString(NSString *string) | |
| { | |
| if (string.length == 0) { | |
| return string; | |
| } | |
| static dispatch_once_t onceToken; | |
| static NSRegularExpression *asteriskRegex = nil; | |
| static NSRegularExpression *questionmMarkRegex = nil; | |
| dispatch_once(&onceToken, ^{ | |
| asteriskRegex = [NSRegularExpression regularExpressionWithPattern:@"\\\\+\\*" options:0 error:NULL]; | |
| questionmMarkRegex = [NSRegularExpression regularExpressionWithPattern:@"\\\\+\\?" options:0 error:NULL]; | |
| }); | |
| NSMutableString *result = [[NSRegularExpression escapedPatternForString:string] mutableCopy]; | |
| NSArray *matches = [asteriskRegex matchesInString:result options:0 range:result.ch_range]; | |
| for (NSTextCheckingResult *match in [matches reverseObjectEnumerator]) { | |
| NSRange range = match.range; | |
| if ((range.length - 2) % 4 == 0) { | |
| [result replaceCharactersInRange:NSMakeRange(NSMaxRange(range) - 2, 2) withString:@".*"]; | |
| } else { | |
| [result deleteCharactersInRange:NSMakeRange(range.location, 2)]; | |
| } | |
| } | |
| matches = [questionmMarkRegex matchesInString:result options:0 range:result.ch_range]; | |
| for (NSTextCheckingResult *match in [matches reverseObjectEnumerator]) { | |
| NSRange range = match.range; | |
| if ((range.length - 2) % 4 == 0) { | |
| [result replaceCharactersInRange:NSMakeRange(NSMaxRange(range) - 2, 2) withString:@"."]; | |
| } else { | |
| [result deleteCharactersInRange:NSMakeRange(range.location, 2)]; | |
| } | |
| } | |
| return [NSString stringWithFormat:@"^%@$", result]; | |
| } | |
| NSString *str1 = @"text/html"; | |
| NSString *str2 = @"text/xhtml"; | |
| NSString *str3 = @"pass****"; | |
| NSString *str4 = @"password"; | |
| NSString *pattern1 = CHRegexPatternForWildcardString(@"text/*"); | |
| NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:pattern1 options:NSRegularExpressionCaseInsensitive error:NULL]; | |
| NSLog(@"[1] => %@", [regex1 numberOfMatchesInString:str1 options:0 range:NSMakeRange(0, str1.length)] ? @"matches" : @"not matches"); // matches | |
| NSLog(@"[2] => %@", [regex1 numberOfMatchesInString:str2 options:0 range:NSMakeRange(0, str2.length)] ? @"matches" : @"not matches"); // matches | |
| NSString *pattern2 = CHRegexPatternForWildcardString(@"text/?html"); | |
| NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:pattern2 options:NSRegularExpressionCaseInsensitive error:NULL]; | |
| NSLog(@"[3] => %@", [regex2 numberOfMatchesInString:str1 options:0 range:NSMakeRange(0, str1.length)] ? @"matches" : @"not matches"); // not matches | |
| NSLog(@"[4] => %@", [regex2 numberOfMatchesInString:str2 options:0 range:NSMakeRange(0, str2.length)] ? @"matches" : @"not matches"); // matches | |
| NSString *pattern3 = CHRegexPatternForWildcardString(@"pass\\*\\*\\*\\*"); | |
| NSRegularExpression *regex3 = [NSRegularExpression regularExpressionWithPattern:pattern3 options:NSRegularExpressionCaseInsensitive error:NULL]; | |
| NSLog(@"[5] => %@", [regex3 numberOfMatchesInString:str3 options:0 range:NSMakeRange(0, str3.length)] ? @"matches" : @"not matches"); // matches | |
| NSLog(@"[6] => %@", [regex3 numberOfMatchesInString:str4 options:0 range:NSMakeRange(0, str4.length)] ? @"matches" : @"not matches"); // not matches |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment