views:

91

answers:

3

Hi,

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

+4  A: 

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

Mikeware
Thanks for your reply Mikeware
Dilshan
+4  A: 
\d+

Match one or more digit.

bourbaki
Thanks for the reply bourbaki
Dilshan
+1  A: 

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.

Maciulis
Expected answer. Works perfectly. Thanks Maciulis. Small correction @".*\\d.*" instead of "@".*\d.*"" otherwise it will result in compiler warning -> Unknown escape sequence \d
Dilshan