【问题标题】:NSRegularExpression on NSString not workingNSString 上的 NSRegularExpression 不起作用
【发布时间】:2012-12-23 02:51:49
【问题描述】:

UITextBoxfield,我插入了一些值,并且我想使用正则表达式匹配字符串..现在我希望文本框文本在我按下按钮时只匹配最多 3 位的数字,然后它应该可以工作.. . 我正在尝试的是哪个不起作用::-

-(IBAction)ButtonPress{

NSString *string =activity.text;
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{1,3}$" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];

 if ([activity.text isEqualToString:modifiedString ])
{ // work only if this matches numeric value from the text box text
}}

【问题讨论】:

  • 您到底想达到什么目标?只允许最多 999 的数字,还是只允许 0、1、2 和 3?

标签: iphone ios regex nsregularexpression


【解决方案1】:

请尝试以下代码。

- (BOOL) validate: (NSString *) candidate {
     NSString *digitRegex = @"^[0-9]{1,3}$";
    NSPredicate *regTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", digitRegex];
    return [regTest evaluateWithObject:candidate];
}

-(IBAction)btnTapped:(id)sender{

    if([self validate:[txtEmail text]] ==1)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Correct id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];

    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Incoorect id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
}

【讨论】:

    【解决方案2】:

    您的代码将所有匹配项替换为一个空字符串,因此如果有匹配项,它将被一个空字符串替换,并且您的检查将永远不会起作用。相反,只需向正则表达式询问第一个匹配的范围:

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{1,3}$" options:NSRegularExpressionCaseInsensitive error:NULL];
    NSRange range = [regex rangeOfFirstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
    
    if(range.location != NSNotFound)
    {
        // The regex matches the whole string, so if a match is found, the string is valid
        // Also, your code here 
    }
    

    您也可以只询问匹配的数量,如果不为零,则字符串包含0999 之间的数字,因为您的正则表达式匹配整个字符串。

    【讨论】:

      【解决方案3】:
      - (BOOL)NumberValidation:(NSString *)string  {
          NSUInteger newLength = [string length];
          NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet];
          NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
          return (([string isEqualToString:filtered])&&(newLength <= 3));
      }
      

      在您的按钮操作事件中,只需像下面这样使用...

      -(IBAction)ButtonPress{
      
       if ([self NumberValidation:activity.text]) {
              NSLog(@"Macth here");
          }
          else {
              NSLog(@"Not Match here");
          }
      }
      

      【讨论】:

      • @Christien 你好兄弟.. 检查这段代码,现在我在我的应用程序中进行测试伙计.. :)
      • ya ryt..了解背后的逻辑
      • 此方法会产生大量开销和大量临时对象。介意向我解释一下使用正则表达式有什么问题吗?
      • 嘿,这没什么错,但在这里我只是使用另一个易于理解的逻辑,并且其他用户也使用常规 epression,所以我只是使用不同的逻辑..
      • @JustSid 哥们,每个人的想法都不一样 :) 我不是说你错了,但你不能指望每个人都有相同和简单的逻辑
      猜你喜欢
      • 2012-04-24
      • 1970-01-01
      • 2011-10-12
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多