2018-01-15 正则表达式判断

.h

//
//  NSString+JM.h
//  JuMiWeiShang
//
//  Created by JM on 2017/7/29.
//  Copyright © 2017年 JM. All rights reserved.
//

#import 

@interface NSString (JM)

/**
 *  获取文字长宽的方法
 */
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize;

/**
 *  获取document路径
 *
 *  @param fileName 文件名
 *
 *  @return string型document路径
 */
+ (NSString *)documentPathWith:(NSString *)fileName;

/**
 *  判断是否是URL
 */
- (BOOL)isUrl;

//====================================
/**
 * 真实姓名必须中文开头且不能大于12位
 */
- (BOOL)isName;

/**
 * 银行卡号(15-20)
 */
- (BOOL)isBankCard;

/**
 * 用户身份证
 */
- (BOOL)isID_Card;
/**
 * 性别验证
 */
- (BOOL)isSex;
/**
 * 用户出生日期
 */
- (BOOL)isBirthday;
/**
 * 用户手机号
 */
- (BOOL)isMobileNumber;
/**
 * 验证码
 */
- (BOOL)isCodeNumber;
/**
 * QQ
 */
- (BOOL)isQQ;
/**
 * email
 */
- (BOOL)isEmail;
/**
 * 经度
 */
- (BOOL)isLongitude;
/**
 * 维度
 */
- (BOOL)isLatitude;
/**
 * 微信号
 */
- (BOOL)isWeiXin;
/**
 * 账号密码
 */
- (BOOL)isPassword;

/**
 * 授权证书号
 */
- (BOOL)isCertificate;

/**
 * 订单号
 */
- (BOOL)isOrderNumber;

//表情符号
- (BOOL)isHasEmoji;
- (BOOL)stringContainsEmoji;

@end

.m

//
//  NSString+JM.m
//  JuMiWeiShang
//
//  Created by JM on 2017/7/29.
//  Copyright © 2017年 JM. All rights reserved.
//

#import "NSString+JM.h"

@implementation NSString (JM)

#pragma mark 获取文字长宽方法
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName:font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

#pragma mark 获取document路径
+ (NSString *)documentPathWith:(NSString *)fileName
{
    
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
}

#pragma mark URL判断
- (BOOL)isUrl
{
    NSString *urlRegex = @"((http|ftp|https)://)?(www.){1}[a-zA-Z0-9]+[.]{1}[\\w]+[/\\w]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}

//====================================
/**
 * 真实姓名必须中文开头且不能大于12位
 */
- (BOOL)isName
{
    NSString *emailRegex = @"^([\u4e00-\u9fa5]){1,10}[a-zA-Z]{0,2}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}

/**
 * 银行卡号(15-20)
 */
- (BOOL)isBankCard
{
    NSString *emailRegex = @"^([0-9]){15,20}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}

/**
 * 用户身份证
 */
- (BOOL)isID_Card
{
    NSString *emailRegex = @"^(\\d{14}|\\d{17})(\\d|[X])$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * 性别验证
 */
- (BOOL)isSex
{
    NSString *emailRegex = @"^(1|2)$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * 用户出生日期
 */
- (BOOL)isBirthday
{
    NSString *emailRegex = @"^([0-9]{8})$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * 用户手机号
 */
- (BOOL)isMobileNumber
{
    NSString *emailRegex = @"^1[34578]([0-9]){9}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * 验证码
 */
- (BOOL)isCodeNumber
{
    NSString *emailRegex = @"^([0-9]{6})$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * QQ
 */
- (BOOL)isQQ
{
    NSString *emailRegex = @"^([0-9]{5,20})$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * email
 */
- (BOOL)isEmail
{
    NSString *emailRegex = @"^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * 经度
 */
- (BOOL)isLongitude
{
    NSString *emailRegex = @"^[0-9]{1,3}.[0-9]{4}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * 维度
 */
- (BOOL)isLatitude
{
    NSString *emailRegex = @"^[0-9]{1,3}.[0-9]{4}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * 微信号
 */
- (BOOL)isWeiXin
{
    NSString *emailRegex = @"^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}
/**
 * 账号密码
 */
- (BOOL)isPassword
{
    NSString *emailRegex = @"^[_0-9a-zA-Z]{6,12}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}

/**
 * 授权证书号 (前两位大写字母,后八位数字)
 */
- (BOOL)isCertificate
{
    NSString *emailRegex = @"^[A-Z]{2}[0-9]{8}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}

/**
 * 订单号
 */
- (BOOL)isOrderNumber
{
    NSString *emailRegex = @"^[0-9]{12}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isValid = [predicate evaluateWithObject:self];
    return isValid;
}

//收货地址,是否有表情
- (BOOL)isHasEmoji
{
    NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}
//是否包含表情
- (BOOL)stringContainsEmoji
{
    __block BOOL returnValue = NO;
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
    {
         const unichar hs = [substring characterAtIndex:0];
         // surrogate pair
         if (0xd800 <= hs && hs <= 0xdbff)
         {
             if (substring.length > 1)
             {
                 const unichar ls = [substring characterAtIndex:1];
                 const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                 if (0x1d000 <= uc && uc <= 0x1f77f)
                 {
                     returnValue = YES;
                 }
             }
         }
         else if (substring.length > 1)
         {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0x20e3)
             {
                 returnValue = YES;
             }
         }
         else
         {
             // non surrogate
             if (0x2100 <= hs && hs <= 0x27ff)
             {
                 returnValue = YES;
             }
             else if (0x2B05 <= hs && hs <= 0x2b07)
             {
                 returnValue = YES;
             }
             else if (0x2934 <= hs && hs <= 0x2935)
             {
                 returnValue = YES;
             }
             else if (0x3297 <= hs && hs <= 0x3299)
             {
                 returnValue = YES;
             }
             else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50)
             {
                 returnValue = YES;
             }
         }
     }];
    return returnValue;
}

@end

身份证的正则写的不是很好。。。。

你可能感兴趣的:(2018-01-15 正则表达式判断)