NSPredicate use in iOS - iphone

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!");
}

Related

ios using filteredArrayUsingPredicate NSPredicate with NSString not working

I'm using to get some of the content of nsmutable array and it work fine if I don't use nsstring to make the query:
NSLog(#"user information %#", [usersInfo filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"%K == 'Joe", #"id"]]);
But try to use a nsstring to query for the user it doesn't work:
NSString *user="Joe";
NSLog(#"user information %#", [usersInfo filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"%K == user", #"id"]]]);
any of you knows what I'm doing wrong? or what would be the best of doing it using NSString to query for users?
When you write
NSString *user = #"Joe";
... [NSPredicate predicateWithFormat:#"%K == user", #"id"]
you seem to expect that "user" in the predicate is replaced by the contents ("Joe") of the NSString variable, but this is not correct.
You have to give the string
as another argument to the predicate and add the %# format that will be expanded by the string.
NSString *user = #"Joe";
... [NSPredicate predicateWithFormat:#"%K == %#", #"id", user]
Here %K (which is var arg substitution for a key path) will be
substituted by the key "id", and %# (which is
var arg substitution for an object value) will be substituted
by the contents of the user variable.
Using %K expansion instead of
[NSPredicate predicateWithFormat:#"id == %#", user]
has the advantage that it works correctly even if the key is a
reserved word in the predicate format string syntax.
I am unable to gues why you are using "%K == 'Joe"
Anyways, You can use predicate as:
[NSPredicate predicateWithFormat:#"object.property LIKE[c] %#", stringValue];
Or,
[NSPredicate predicateWithFormat:#"object.property==[c] %#", stringValue];
try this:
NSString *user="Joe";
NSLog(#"user information %#", [usersInfo filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"SELF contains [cd] %#", user]]]);
Make your predicate format string before don't try to add property name inside predicate format :
NSString *predicateFormat = [NSString stringWithFormat:#"%# == %%#",#"id"];
NSPredicate *predicate = [NSPredicate predicateWithFormat: predicateFormat, #"Joe"];
NSArray *filteredArray = [mutableArray filteredArrayUsingPredicate:predicate];
Use:
NSLog(#"user information %#", [usersInfo filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:#"%K == '%#'",#"id",user]]]);
When you use:
NSLog(#"user information %#", [usersInfo filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"%K == user", #"id"]]]);"
The user will be a part of the string, it won't replace with the content of the NSString object.

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];

NSCompoundPredicate fails to match

I'm building a NSPredicate using the code below for an iPhone app. The logging shows the prediate to be: location CONTAINS "head" AND shape CONTAINS "oval" AND texture CONTAINS "bumpy" AND colour CONTAINS "red"
I get no results. If I limit the predicate to a single item it will work, more than 1 fails.
Can anyone tell me why?
Many thanks
NSMutableArray *subPredicates = [[NSMutableArray alloc] init];
for (Ditem in self.tableDataSource) {
NSString *Title = [Ditem valueForKey:#"Title"];
NSString *Value = [Ditem valueForKey:#"Value"];
if([[Value lowercaseString] isEqualToString: #"all"]){
Value = #"";
}
else{
NSPredicate *p = [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:[Title lowercaseString]] rightExpression:[NSExpression expressionForConstantValue:[Value lowercaseString]] modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:0];
[subPredicates addObject:p];
}
}
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
NSLog(#"predicate: %#", predicate);[self.fetchedResultsController.fetchRequest setPredicate:predicate];
Your predicate is requiring that all of the values in your filterable objects be strings. Is that correct?
Also, I would simplify your subpredicate creation to:
NSPredicate * p = [NSPredicate predicateWithFormat:#"%K CONTAINS %#", [Title lowercaseString], [Value lowercaseString]];

NSPredicate usage

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