Regular Expressions with Parse 22 Dec 2011
When writing mobile applications it's important to minimize the amount of data sent over the network. Our goal with Parse is to make this easy, so your app runs as fast as possible. Along these lines, we've just launched regular expression filtering with Parse queries.
You can now provide any regular expression to a Parse query and filter the return data based on it. For example, let's say you're storing full names in the name field of a user, and you want to search for all users whose name starts with Jimbo and log their full name. To fetch this data in iOS:
PFQuery *query = [PFQuery queryForUser];
// Require that name start with Jimbo
[query whereKey:@"name" matchesRegex:@"^Jimbo"];
// Issue the query
[query findObjectsInBackgroundWithBlock:^(NSArray *users, NSError *error) {
if (error) return;
// Users now contains the users named Jimbo.
for (PFUser *user in users) {
NSLog(@"name: %@", [user objectForKey:@"name"]);
}
}];
There are also helper functions for the specific cases of matching prefixes, suffixes, and containing a string.
// A simpler way to require that name start with Jimbo
[query whereKey:@"name" hasPrefix:@"Jimbo"];
// Requires that name ends with Jenkins
[query whereKey:@"name" hasSuffix:@"Jenkins"];
// Accepts any address containing "Parker St"
[query whereKey:@"address" containsString:@"Parker St"];
This functionality is now available for iOS, Android, and via the REST API. Read more about it in the iOS documentation, Android documentation, or REST documentation.
Your feedback is always really helpful to us as we figure out what we should build next. We're always listening, so let us know what you're building on Parse, and how we can help you build your app. Get in touch at feedback@parse.com.
Thanks for reading!