2016-05-28 69 views
1

在下面的方法中,我想檢查一個UITextField文本如果: 它包含一個或多個英文單詞,以及一個或多個數字並讓其中包含特殊字符(!@ $ &#) 返回true,否則返回false。ios中的正則表達式(一個字母和特殊字符)

#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; 
} 

感謝

+0

你有什麼問題?你只需要顯示文字或數字? –

+0

你實際上不允許有0個或更多的符號。使用'@「^ [a-zA-Z0-9 \\ u0021 \\ u0040 \\ u0023 \\\\\\\\\\\ * * * * *''。如果你想確保有一個字母和一個數字,使用'@「^(?= [^ a-zA-Z] * [a-zA-Z])(?= \\ D * \\ d) [A-ZA-Z0-9 \\ u0021 \\ u0040 \\ u0023 \\ u0024 \\ u0026] * $「' –

回答

3

說明

^(?=.*[a-z])(?=.*[0-9])[[email protected]$&#]*$ 

Regular expression visualization

這個正則表達式將執行以下操作:

  • (?=.*[a-z])要求日E弦到包含至少一個a-z字符
  • (?=.*[0-9])需要對包含至少一個0-9字符
  • [[email protected]$&#]*$允許進行字符串最多隻有a-z字符,0-9字符和[email protected]$&#符號
的串

示例

實時演示

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" 
---------------------------------------------------------------------- 
相關問題