NSPredicate usage - iphone

I am having string #"King (+$100)".I want to use NSPredicate to find if string is having (+$[0-9]*).So how to use NSPrdicate

Try this:
// The string to evaluate
NSString *string = #"King (+$100)";
// The regular expression
NSString *regExp = #".*\\(\\+\\$[0-9]*\\).*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF MATCHES %#",regExp];
// Test if the object checks the regular expression
BOOL ok = [predicate evaluateWithObject:string];
For all documentation on the NSPredicate see:
NSPredicate Programming Guide

Related

Alter reg-ex for email Validation on iphone

I am trying to validate email using reg-ex. here is the code...
+ (BOOL) stringIsValidEmail:(NSString *)checkString;
{
NSString *emailRegEx =
#"(?:[a-zA-Z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
#"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
#"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-"
#"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
#"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
#"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
#"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];
return [emailTest evaluateWithObject:checkString];
}
now I don't have much knowledge about regex but this accepts a#a.c as a valid email. But this should not be the case and at least two characters should be required at the end. What paramater do I need to change in this so it returns false. I have hit and tried but that didn't work. Thanks for your help.
Too much symbols, you can try this
- (BOOL) IsValidEmail:(NSString *)checkString {
BOOL sticterFilter = YES;
NSString *stricterFilterString = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSString *laxString = #".+#.+\\.[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = sticterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:checkString];
}
Try using this as regex
NSString *emailRegex = #"[A-Z0-9a-z._]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
the {2,4} especially validates that the ending characters should be alphabets and more than two in count

NSPredicate not working

I have this string:
<td align="right"><span> 19:45 </span></td>
I want to use a NSPredicate on it to search for the 19:45 part but every possible combination I tried returns nothing! I'm kinda losing my marbles here so please help!
Things i've tried:
NSString *timeStringPredicate = #"[0-9]:[0-9]";
NSPredicate *timeSearch = [NSPredicate predicateWithFormat:#"SELF like %#", timeStringPredicate];
if ([timeSearch evaluateWithObject:dayText]) {
NSLog(#"This is a time");
}
Or in these possibilities:
NSString *timeStringPredicate = #"[0-9]\\:[0-9]";
NSString *timeStringPredicate = #"*[0-9]:[0-9]*";
NSString *timeStringPredicate = #"*[0-9]\\:[0-9]*";
NSString *timeStringPredicate = #"*.[0-9]:[0-9].*";
NSString *timeStringPredicate = #"*.[0-9]\\:[0-9].*";
And about everything else.
Help!
like doesn't use regexp syntax. For that, you need to use matches instead. See The Predicate Programming Guide for details.
NSString *timeStringPredicate = #".*\\:[0-9].*";
NSPredicate *timeSearch = [NSPredicate predicateWithFormat:#"SELF matches %#", timeStringPredicate];
Try to do this:
NSString *timeStringPredicate = #".*\\:[0-9].*";
NSPredicate *timeSearch = [NSPredicate predicateWithFormat:#"SELF matches '%#'", timeStringPredicate];

NSPredicate use in iOS

Can anyone knows how to use NSPredicate for below format?
[Any letter][Any Number][Any letter][space][Any Number][Any letter][Any Number]
I want to validate string fot above fromat.
Thanks.
Use this format.
NSString *str1 = #"a8D 9k3";
NSString *str2 = #"a8 9k3";
NSString *testFormat = #"[a-zA-z][0-9][a-zA-z] [0-9][a-zA-z][0-9]";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF matches %#", testFormat];
Following is valid:
BOOL isValid = [predicate evaluateWithObject:str1];
Following is invalid:
BOOL isValid = [predicate evaluateWithObject:str2];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF MATCHES[c] %#", #"[a-z][0-9][a-z] [0-9][a-z][0-9]"];
if ([pred evaluateWithObject:#"a3B 5C9"])
{
NSLog(#"It matches!");
}

Creating NSPredicate from string

I need to form a predicate from multiple arrays of values. So I thought I can initially form a string with all the values, and then pass that string for usage in a predicate, i.e
NSString* stringForPredicate = [[NSString alloc] init];
if (fromDate != nil) {
stringForPredicate = [stringForPredicate stringByAppendingFormat:#"(Date > %# AND Date < %#)", fromDate, toDate];
}
There are further calculations that I do to form the final predicate string.
Now, I want the predicate to use this string. I thought that something like this would work:
NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:#"%#",stringForPredicate];
But it doesnt and throws the exception:
'Unable to parse the format string "%#"'
Is there a way I can make this work?
thanks,
The stringForPredicate variable actually contains the format_string. So, you need to assign that variable in place of the format_string, and pass the args after that, seperated by commas, like this.
NSSring *stringForPredicate = #"(Date > %# AND Date < %#)";
NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:stringForPredicate, fromDate, toDate];
For compound predicates:
NSMutableArray *subPredicates = [NSMutableArray array];
if (fromDate != nil) {
NSPredicate *from_predicate = [NSPredicate predicateWithFormat:#"Date > %#", fromDate];
[subPredicates addObject:from_predicate];
}
if (toDate != nil) {
NSPredicate *to_predicate = [NSPredicate predicateWithFormat:#"Date < %#", toDate];
[subPredicates addObject:to_predicate];
}
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
Wont this do it?
NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:stringForPredicate];
or you have to do this
NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"%#",stringForPredicate]];
From the reference documents
Format String Summary
It is important to distinguish between
the different types of value in a
format string. Note also that single
or double quoting variables (or
substitution variable strings) will
cause %#, %K, or $variable to be
interpreted as a literal in the format
string and so will prevent any
substitution.
Try this
stringForPredicate = [stringForPredicate stringByAppendingFormat:#"(Date > %#) AND (Date < %#)", fromDate, toDate];
For those using swift:
let predicate = NSPredicate.init("Date > %# AND Date < %#", fromDate, toDate);

Help creating a predicate for use with filteredArrayUsingPredicate

I am trying to learn how to use predicates and so am trying to replace the following working code with filteredArrayUsingPredicate...
[filteredLocations removeAllObjects];
for (NSString *location in locations) {
NSRange range = [location rangeOfString:query options:NSCaseInsensitiveSearch];
if (range.length > 0) {
[filteredLocations addObject:location];
}
}
Instead I am trying....
[filteredLocations removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains %#", searchText];
[filteredLocations addObjectsFromArray: [locations filteredArrayUsingPredicate:predicate]];
I am not getting the same results with the predicate as I am with for loop rangeOfString. With the range of string for example searchText returns an 8 item array while with the same value returns only 2 with the predicate. Another example, hono will find honolulu in the locations array while it will not find anything using the predicate.
As I understand it SELF represents the object object being evaluated ie. the locations array, so I think that is the correct syntax.
Any help would be appreciated
Thanks, John
I thought that in your for loop, you specify an option NSCaseInsensitiveSearch but in the predicate you didn't.
You can try with this one
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(SELF contains[cd] %#)", searchText];
More details can be looked at here
NSPredicate Tutorial