ios中的正则表达式(一个单词一个字母和特殊字符)
Regular Expressions in ios(a word a letter and special characters)
在下面的方法中,我想检查 UITextField
文本,如果:
它包含一个或多个英文单词和一个或多个数字,并且可选地包含特殊字符(!@$)
return 正确,否则 return 错误。
#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"
----------------------------------------------------------------------
在下面的方法中,我想检查 UITextField
文本,如果:
它包含一个或多个英文单词和一个或多个数字,并且可选地包含特殊字符(!@$)
return 正确,否则 return 错误。
#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"
----------------------------------------------------------------------