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.
Related
NSRegularExpression* regExp = [[NSRegularExpression alloc]
initWithPattern:#"^[-+s]*[0-9s]+$"` options:NSRegularExpressionCaseInsensitive
error:nil];
I used above expression. but it accepts the + and - values. but the problem is, it accepts more than one sign before the integer number.
1)+++++12
2)--++12
these cases are also accepted.
What can use , it should accept only one sign before integer?
Thanks in Advance
I think you're looking for #"^[-+]?[0-9]+$".
update
I think you are using 's' to match whitespace. That will not work.
#"^\\s*[-+]?\\s*[0-9]+\\s*$" will match whitespace before the plus/minus, between the plus/minus and the digits, and after the digits.
NOTE: a ? matches 0 or 1 of the previous groups, in this case [-+].
UPDATE: Thanks #ValeriyVan for suggesting a fix for a critical issue is my whitespace code. I don't know why the community rejected your edit.
Following code for the checking the integer values.
- (BOOL) validateIntegerValue: (NSString *) candidate {
NSString *Regex = #"^[+-]{0,1}[0-9]+$";
NSPredicate *intTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", Regex];
return [intTest evaluateWithObject:candidate];
}
In my iPhone app, I'm reading a csv file. The relevant line is this:
NSString *countrycode = [[[NSString alloc] initWithFormat:#"%#", [arr objectAtIndex:2]]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
This returns "CN" (which stands for China).
When I do this:
NSLog(#"Manual: %#, country code: %#",#"CN",countryCode);
I get:
Manual: CN, country code: "CN"
One has quotes and the other does not. I don't know why this is.
The reason this is tripping me up is the following:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"countrycode == %# ", #"CN"];
This works fine, and returns China from Core Data.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"countrycode == %# ", countrycode];
This fails to return anything. I am assuming this is because it has quotes around it, or something, although perhaps I am incorrect.
What am I doing wrong here?
Actually the correct way to format a predicate to exclude quotes is the to use %K versus %#. See Predicate Format String Syntax.
Your countryCode variable must have quotes inside of it when it's read back. The first time you assign the literal #"CN" the quotes are removed as they specify that your variable is an NSString. They aren't really inside of the literal string. If you wanted strings inside of the first CN, you'd need to explicitly specify the quotation marks, e.g. #"""CN"""
However, if you want to get rid of any quotations in the second string, you could always do this to the string prior to putting it into your predicate:
[countryCode stringByReplacingOccurrencesOfString:#"""" withString:#""];
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
I have:
NSString *promise = #"thereAreOtherWorldsThanThese";
which I'm trying to transform into the string:
#"There are other worlds than these"
I'm guessing this is a regex job, but I'm very new to Objective C, and have so far had no luck. I would greatly appreciate any help!
I'd use GTMRegex (http://code.google.com/p/google-toolbox-for-mac/), for example:
NSString *promise = #"thereAreOtherWorldsThanThese";
GTMRegex *regex = [GTMRegex regexWithPattern:#"([A-Z])"];
NSLog(#"%#", [[regex stringByReplacingMatchesInString:promise
withReplacement:#" \\1"] lowercaseString]);
As for removing the uppercase letters you can simply use lowercaseString on NSString.
But as for inserting spaces just before an uppercase letter, I would agree that it would be a job for a regex, and sadly, my regex fu is rubbish :)
Without using any libraries you can use this NSString category I posted. Just perform lowerCaseString on the string array.
How do I convert an NSString from CamelCase to TitleCase, 'playerName' into 'Player Name'?
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]];