开发者

Regular expression for numbers

开发者 https://www.devze.com 2023-01-21 17:25 出处:网络
I need a regular expression to detect 开发者_StackOverflowat least one number in a string. Other characters can be anything. Please help me to implement this in objective C.

I need a regular expression to detect 开发者_StackOverflowat 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消