在IOS中捕获匹配的字符串 - 匹配由NSPREDICATE完成

时间:2012-06-28 06:39:16

标签: objective-c

我有一个代码来匹配正则表达式和我的string.once我执行代码,结果表明匹配是YES。

任何人都可以帮助我在其他位置单独提取匹配的字符串

这是我的代码

    (void)viewDidLoad 
    {
    NSString *string = @"@002@[00152-614[4000846555@";

    NSString *expression = @"[\-@0]{1}[0-9]{3}[\-@]{1}[\[0]{1}[0-9]{5}[\-0]{1}[0-9]{3,}[\[0]{1} [0-9\-]{7,}[@0]{1}";

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", expression];

    BOOL match = [predicate evaluateWithObject:string];

    if(match) NSLog(@"match is YES") ;

    else NSLog(@"match is NO") ;

  [super viewDidLoad];

 }

提前致谢

此致

Praveenkumar

1 个答案:

答案 0 :(得分:0)

试试这个

 - (BOOL) validateEmail: (NSString *)candidate {
   NSString *emailRegex = @"[\-@0]{1}[0-9]{3}[\-@]{1}[\[0]{1}[0-9]{5}[\-0]{1}[0-9]{3,}[\[0]{1} [0-9\-]{7,}[@0]{1}"; 
   NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 
   return [emailTest evaluateWithObject:candidate];
  }