ios - iOS中的正则表达式(单词,字母和特殊字符)

标签 ios objective-c regex nsregularexpression

在以下方法中,如果要检查以下内容,我想检查UITextField文本:
它包含一个或多个英语单词,以及一个或多个数字,并让其可选地包含特殊字符(!@ $&#)
返回true,否则返回false。

#define pattern   @"^[a-zA-Z0-9\\u0021\\u0040\\u0023\\u0024\\u0026]{1}*$"

- (BOOL)stringHasCorrectFormat:(NSString *)str {

      if ([str componentsSeparatedByString:SPACE].count > 1)
         return NO;
      NSString *regularPattern = EMPTY_STRING;
      regularPattern = [regularPattern stringByAppendingString:pattern]
      NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:regularPattern
                              options:NSRegularExpressionCaseInsensitive
                              error:nil];



      NSTextCheckingResult *match = [regex
                               firstMatchInString:str
                               options:0
                               range:NSMakeRange(0, [str length])];


     if (match != nil) {
         return YES;
     }

     return NO;
}

谢谢

最佳答案

描述

^(?=.*[a-z])(?=.*[0-9])[a-z0-9!@$&#]*$

此正则表达式将执行以下操作:
  • (?=.*[a-z])要求该字符串包含至少一个a-z字符
  • (?=.*[0-9])要求该字符串包含至少一个0-9字符
  • [a-z0-9!@$&#]*$允许字符串仅由a-z字符,0-9字符和!@$&#符号
  • 组成



    现场演示

    https://regex101.com/r/mC3kL3/1

    说明
    NODE                     EXPLANATION
    ----------------------------------------------------------------------
      ^                        the beginning of a "line"
    ----------------------------------------------------------------------
      (?=                      look ahead to see if there is:
    ----------------------------------------------------------------------
        .*                       any character except \n (0 or more times
                                 (matching the most amount possible))
    ----------------------------------------------------------------------
        [a-z]                    any character of: 'a' to 'z'
    ----------------------------------------------------------------------
      )                        end of look-ahead
    ----------------------------------------------------------------------
      (?=                      look ahead to see if there is:
    ----------------------------------------------------------------------
        .*                       any character except \n (0 or more times
                                 (matching the most amount possible))
    ----------------------------------------------------------------------
        [0-9]                    any character of: '0' to '9'
    ----------------------------------------------------------------------
      )                        end of look-ahead
    ----------------------------------------------------------------------
      [a-z0-9!&#]*             any character of: 'a' to 'z', '0' to '9',
                               '!', '&', '#' (0 or more times (matching
                               the most amount possible))
    ----------------------------------------------------------------------
      $                        before an optional \n, and the end of a
                               "line"
    ----------------------------------------------------------------------
    

    关于ios - iOS中的正则表达式(单词,字母和特殊字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37499037/

    相关文章:

    ios - Objective-C 中的多态性和继承示例

    iOS 如何在没有设备的情况下创建开发配置?

    ios - 为什么使用 UIImageJPEGRepresentation 方法通过 writetofile 保存的 .jpeg 文件比 ios 中的 UIImageWriteToSavedPhotosAlbum 大

    iphone - 如何在 iOS 中更改 URL 中的内容?

    ios - 获取 UILabel 枚举子串的位置

    iphone - 表格 View 单元格按钮与其他单元格混淆?

    objective-c - Hpple 实现/无法识别的选择器

    regex - 为什么不是//和m//完全同义?

    正则表达式替换 Linux shell 脚本中的密码?

    python - 删除中间带有大写字母的单词