0

我有一个代码来匹配正则表达式和我的字符串。一旦我执行代码,结果显示匹配是 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];

 }

提前致谢

问候

普拉文库马尔

4

1 回答 1

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];
  } 
于 2012-06-28T07:20:05.860 回答