常用的正则表达式

来源:互联网 发布:微信聊天数据恢复 编辑:程序博客网 时间:2024/05/16 04:55
    我们在开发的时候,很多时候都会用到正则表达式。它的便利,我就不多说啦!下面是一些常用的正则表达式,希望对各位有用。
手机号:"(\\+\\d+)?1[34578]\\d{9}$"
邮箱:"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
全数字:"^[0-9]*$"
全小写字母:"^[a-z]+$"
全大写字母:"^[A-Z]+$"
身份证:"^(\\d{14}|\\d{17})(\\d|[xX])$"
只能是汉字和字母:"^[a-zA-Z\u4e00-\u9fa5]+$"
只能是数字和汉字:"[0-9\u4e00-\u9fa5]*"
只能是数字和字母:"^[0-9a-zA-Z]+$”
只能是数字、汉字和字母:"[a-zA-Z0-9\u4e00-\u9fa5]*”
只能是字母:"[A-Za-z]+"
实例1:简单示范
    NSString *string =@"123";
    NSString *checkString =@"123";
    NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF = %@",string];
    if([predicateevaluateWithObject:checkString]){
        NSLog(@"======>>>%@==%@",checkString,predicate);
    }
输出:
 ======>>>123==SELF == "123"

实例2:在数组中的应用
  NSArray *testArr =@[@"432",@"refaf12",@"cd",@"334",@"3424",@"9999",@"颠覆"];
    NSString *test =@"^[0-9]*$";
    NSLog(@"===%@===%@",[testArrfilteredArrayUsingPredicate:[NSPredicatepredicateWithFormat:@"SELF MATCHES %@",test]],[NSPredicatepredicateWithFormat:@"self matches %@",test]);
输出:
===(
    432,
    3424,
    9999
)===SELF MATCHES "^[0-9]*$"

原创粉丝点击