I have phone numbers stored in database in the format like +1-541-634-4210 or +1 (541) 634-4210.
I want to search unformatted number like +15416344210 in the database by using NSPredicate but i unable to form the correct predicate format.
I try something like this
NSString *regex = [NSString stringWithFormat:#"+0123456789"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:#"number MATCHES %# == %#",regex,phoneNumber]];
So any ideas on how to achieve that?
Any help is much appreciated. Thanks!
Have you tried using predicate.
Swift
let predicate = NSPredicate(format: "SELF MATCHES %#", "[+][0-9]+")
predicate.evaluateWithObject(phone number)
Objective-C
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", "[+][0-9]+"];
[predicate evaluateWithObject:phoneNumber];
Please update the right string based on your conditions.
Try this:
predicate = [NSPredicate predicateWithFormat:#"number MATCHES %#", [[filter componentsSeparatedByString:#"- "] objectAtIndex:0]];
Related
How do i write a predicate to filter out data with a search string like this.
Users enters "red pepper".
This should return all the records including, "red pepper", "red bell pepper" etc
This is what i have written.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(ingredientName contains[c] %#)" ,self.searchIngrediants.text ];
[fetchRequest setPredicate:predicate];
This only returns records that contains the search term such as "red pepper", "belgian red pepper".
Is it possible to do this with a predicate. Im using Objective-c.
Any help would be much appreciated!
For that you need to use LIKE.
NSString *searchStringWithPattern = [self.searchIngrediants.text stringByReplacingOccurrencesOfString:#" " withString:#"*"];
searchStringWithPattern = [NSString stringWithFormat:#"*%#*", searchStringWithPattern];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(ingredientName LIKE[cd] %#)" , searchStringWithPattern];
[fetchRequest setPredicate:predicate];
Objective C Code:-
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:#"ingredientName contains[cd] %#",self.searchIngrediants.text];
Contains will check the character is there or not.
Swift Code :-
var predicate: NSPredicate?
predicate = NSPredicate(format: "ingredientName contains[cd] %#", searchIngrediants.text)
With regular expression it should be something like:
NSString* regex = #"red.*pepper";
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"(ingredientName MATCHES %#)", regex];
If your input has newlines/TABs/... adapt the regex accordingly.
I'm dealing with a document management page. I used NSPredicate regular expression while got the searchBar.text from user inputs. An error occurred (as the image below) while the inputs are brackets in Chinese form but went well in English form. If anyone has come across this matter? Thank you.
Here is my code,crash happen to the last row.
NSString * searchStr = [NSString stringWithFormat:#"fileName like[cd] \'*%#*\'",searchDisplayController.searchBar.text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:searchStr];
self.fileArrayAfterPre = [[NSMutableArray alloc] initWithArray:[fileArr filteredArrayUsingPredicate:predicate]];
LIKE: It is use in wildcard expression.
CONTAIN: According to your question you should use the contain[cd] because it match the file with search text.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"fileName CONTAINS[cd] %#",searchDisplayController.searchBar.text];
self.fileArrayAfterPre = [[NSMutableArray alloc] initWithArray:[fileArr filteredArrayUsingPredicate:predicate]];
I would like to filter an NSArray of NSDictionaries, however I would like to filter the result using one, two or even three NSPredicate values?
Currently I am filtering my array by doing this.
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"parts == %#", filterString];//keySelected is NSString itself
NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[currentParts filteredArrayUsingPredicate:predicateString]];
sortedItemsArray = [filteredArray mutableCopy];
But I am not sure how I would do this using two predicates?
The other two predicates individually look like the one above accept different keys.
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"area == %#", filterString];
and
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"item == %#", filterString];
What I was thinking is that maybe you could have something like
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"stage == %# area == %#", filterString, areaflterstring];
But I don't think that's possible.
It is possible, but you need to tell the predicate how to combine the parts, like:
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"stage == %# AND area == %#", filterString, areaflterstring];
You can alternatively use NSCompoundPredicate to combine a number of predicates.
I want to query sqllite table by specifying a range. So it's like give me all records whose id column between 3000 and 3010.
I tried what Apple recommends, but it didn't work. Here is what I tried and failed.
NSPredicate *betweenPredicate =
[NSPredicate predicateWithFormat: #"attributeName BETWEEN %#", #[#1, #10]];
I have 2 strings called start and end. I updated Apple's example as following.
NSPredicate *betweenPredicate =
[NSPredicate predicateWithFormat: #"%# BETWEEN %#", columnName, #[start,end]];
When I executeFetchRequest with the above predicate I get 0 records even though the table has records matching the predicate. Can someone point where I go wrong?
You have to use the %K format specifier for attribute names:
[NSPredicate predicateWithFormat: #"%K BETWEEN %#", columnName, #[start,end]];
If that does not work (I have never used "BETWEEN" for Core Data fetch requests), you
could replace it by the equivalent predicate
[NSPredicate predicateWithFormat: #"%K >= %# AND %K <= %#",
columnName, start, columnName, end];
I have to use NSPredicatefor search through some Core Data objects. But the key nameof the custom object could contains both numbers then letters.
The string could look like:John 1234 Lennonor Ringo Starr.
I normally would use the predicate NSPredicate *predicate = [NSPredicate predicateWithFormat:#"Any name CONTAINS[cd] %#",searchString];
But, if I search for John Lennon the predicate don't return anything, because it can't compare if it contains the characters John Lennon, since is missing the 1234. Any tips what kind of predicate I can use?
You can tokenise your query , maybe as simple as
NSArray *tokens = [querystring componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Then construct a compound predicate.
NSMutableArray *predarray = [NSMutableArray array];
for(NSString *token in tokens)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"Any name CONTAINS[cd] %#",token];
[predarray addObject:predicate];
}
NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:predarray];
And feed that to your query
In real life I would run a bit of validation against each token to check its going to make a valid predicate and wont crash or create a security risk. e.g Strip special chars like "* []"
EDIT:Corrected predicate type to work with questions situation.
Try using LIKE instead of contains, and then you can use wild cards, for instance John*Lennon should match a string that starts with John and ends with Lennon with any number of other characters in between. You can use ? Instead which would match only one character for each question mark if you want more control over what's matched.
You could split the search string up into an array of strings and then switch your predicate to look for any string in the name:
NSArray *strings = [searchString componentsSeparatedByString:#" "];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"ANY %# IN name",strings];