Skip to content

Instantly share code, notes, and snippets.

@Adam0101
Created May 19, 2011 15:42
Show Gist options
  • Save Adam0101/981063 to your computer and use it in GitHub Desktop.
Save Adam0101/981063 to your computer and use it in GitHub Desktop.
sanitize search twitter
//Create the regular expression to match against whitespace or % or ^
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\s|%|^)" options:NSRegularExpressionCaseInsensitive error:&error];
//Create the regular expression to match against hash tags
NSRegularExpression *regexHash = [NSRegularExpression regularExpressionWithPattern:@"#" options:NSRegularExpressionCaseInsensitive error:&error];
// create the new string by replacing the matching of the regex pattern with the template pattern(whitespace)
NSString *tempString = [regex stringByReplacingMatchesInString:searchTerm options:0 range:NSMakeRange(0, [searchTerm length]) withTemplate:@"%20"];
// create the new string by replacing the matching of the regex pattern with the template pattern(whitespace)
NSString *hashTempString = [regexHash stringByReplacingMatchesInString:tempString options:0 range:NSMakeRange(0, [searchTerm length]) withTemplate:@"%23"];
searchTerm = hashTempString;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment