1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
-(NSString*)replaceDomain:(NSString*)str{ NSMutableString *tempString = [NSMutableString stringWithString:str]; NSError *error; NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)"; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *arrayOfAllMatches = [regex matchesInString:tempString options:0 range:NSMakeRange(0, [tempString length])]; NSString *substringForMatch = [NSString string]; for (NSTextCheckingResult *match in arrayOfAllMatches) { substringForMatch = [tempString substringWithRange:match.range]; NSLog(@"substringForMatch: %@",substringForMatch); } [tempString replaceOccurrencesOfString:substringForMatch withString:@"yourdomain.cOm" options:NSBackwardsSearch range:NSMakeRange(0, [tempString length])]; return tempString; } |
转载请注明:天狐博客 » 非常好用的iOS正则表达式替换字符串包含的域名