NSPredicates beginsWith - iphone

I have an NSArray whose contents are strings with a format similar to:
[A-z]{+}-[0-9]{+}
so basically a bunch of repeating alpha characters, a separator, and then 1 or more digits so
I want to filter by values in the array that match up to the separator but I can't seem to explicitly specify it in my predicator's format:
NSPredicate *aPredicate = [NSPredicate predicateWithFormat:#"self BEGINSWITH %#", aValue];
NSArray *filtered = [entries filteredArrayUsingPredicate:aPredicate];
How do you constrain the filtering for such a case?

You could use the "MATCHES" operator to do a regular expression search, like so:
NSPredicate * p = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", #"[a-z]+-.*"];
NSArray * s = [NSArray arrayWithObject:#"abc-123"];
NSLog(#"%#", [s filteredArrayUsingPredicate:p]);
There is a caveat, though. The regular expression is matched across the entire string. So if you want to find all the elements that begin with 3 letters, your expression can't just be "[a-z]{3}". It has to be "[a-z]{3}.*". The first will fail for anything that's not 3 letters, whereas the second will match anything that's at least 3 letters long.
Took me a while to realize this...

You probably want to use the MATCHES operator that lets you use Regular Expressions.
See Predicate Programming Guide:Regular Expressions

Related

NSPredicate with Underscore Crash

fetchRequest.predicate = [NSPredicate predicateWithFormat:#"identifer==1_12"];
gives me :
Unable to parse the format string "identifier==1_12"'
I have tried using MATCHES, LIKE, =, with spaces ==, without spaces etc. Somehow I feel the underscore is some kind of a reserved sign.
Any help?
If you want to compare with the string "1_12", you have to enclose it in single quotes:
[NSPredicate predicateWithFormat:#"identifer == '1_12'"]
Alternatively:
[NSPredicate predicateWithFormat:#"identifer == %#", #"1_12"]

Find occurrence of substring in sentence only if word starts with substring

If I have 2 strings:
"My name is Eric"
"America is great"
And my substring is 'eri'.
How can I write a function that will only return true for the first sentence, because it has a WORD that starts with eri (Eric) and does not just contain eri (AmERIca).
Note;
I have been using NSPredicates, but using CONTAINS would return both, and using BEGINSWITH would only check the first word.
This string is also contained in an Object, say and the property is called ,
so my current code is:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF.name contains[cd] %#",
searchText];
searchResults = [[myArray filteredArrayUsingPredicate:resultPredicate] mutableCopy];
Why not search for a string with leading space, like " eri"? That will automatically only find actual words and not something in the middle.

Regular expression for numbers

I need a regular expression to detect at least one number in a string. Other characters can be anything. Please help me to implement this in objective C.
Regards,
Dilshan
\d+
Match one or more digit.
This is a very similar question to:
Regular Expressions in Objective-C and Core Data
Check ICU Regex Documentation for figuring out your regular expression needs
To match a digit anywhere in string use .*\\d.*. To implement in objective-c use NSPredicate try something like this:
NSString *matchphrase = #".*\\d.*";
BOOL match = NO;
NSString *item = #"string with d1g1it";
NSPredicate *matchPred = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", matchphrase];
match = [matchPred evaluateWithObject:item];
More here
Edited according Dislhan comment.

NSPredicate of special characters - iPhone

I'm trying to make a predicate that includes special characters
For example:
[[myIngredients filteredSetUsingPredicate:[NSPredicate predicateWithFormat:#"name BEGINSWITH[c] %#", [alphabet objectAtIndex:idx]]];
Here I will get all the ingredient which starts with (let say for idx = 5) 'e'. As I have to do my app in english and french, some ingredients start with special character like 'é' or even 'œ' for 'o'. How can I include these special characters in my predicate?
Best
I think you might be looking for the “diacritic insensitive” flag that NSPredicate supports. It’s just like the “c” flag you’re already using, except you use a “d”. Like so:
… predicateWithFormat:#"name BEGINSWITH[cd] %#", …
Now the string “e” will also match “é”, “ê”, “ë”, and so on.

How to perform a case and diacritic insensitive filter of an array of NSDictionaries (not NSString)?

I have an array of dictionaries. I would like to filter that array by seeing if the #"name" field of each dictionary contains a given string.
The catch is that I would like to make my filtering insensitive to case and diacritics.
If the array contained only strings I could easily use an NSPredicate. However, it doesn't, and I don't see a way that NSPredicate can accomodate this situation.
If I only cared about case-insensitivity, I could loop through all the items and compare the lowercased filter string to the lowercased name. But I don't know of a similar trick for diacritics.
Is there a good solution to this somewhere?
Check the top answer on this question:
Non US characters in section headers for a UITableView
You should be able to use that code to get rid of the diacritics and then do a case insensitive compare or search.
What about something like:
NSArray * array = .....
NSString * searchString = #"foo";
NSArray * filteredArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"name contains[cd] %#", searchString]];