正则表达式校验

2021-11-04  本文已影响0人  林希品

证件号:正则表达式

/** 正则表达式:验证身份证 */

    public static final String REGEX_ID_CARD = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +

            "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

    /** 正则表达式:验证户口簿 9位数字 */

    public static final String REGEX_HUKOU_CARD = "\\d{9}";

    /** 正则表达式:验证护照 数字+字母,共9位 */

    public static final String REGEX_PASSPORT_CARD = "^([a-zA-z]|[0-9]){9}$";

    /** 正则表达式:验证军官证 汉字+8位数字 */

    public static final String REGEX_OFFICER_CARD = "^[\\u4E00-\\u9FA5](字第)([0-9a-zA-Z]{4,8})(号?)$";

    /** 正则表达式:验证驾驶证 12位数字 */

    public static final String REGEX_DRIVE_CARD = "\\d{12}$";

    /** 正则表达式:验证港澳居民通行证 H/M + 10位或8位数字 */

    public static final String REGEX_HK_CARD = "^[HMhm]{1}([0-9]{10}|[0-9]{8})$";

    /** 正则表达式:验证台湾居民通行证 新版8位或18位数字,旧版10位数字 + 英文字母 */

    public static final String REGEX_TW_CARD = "^\\d{8}|^[a-zA-Z0-9]{10}|^\\d{18}$";

OC 校验方法

.h

/// 正则表达式校验

/// @param patternStr 正则表达式

/// @param stringStr 被校验字符串

+ (BOOL)checkStringWithPattern:(NSString*)patternStrstringStr:(NSString*)stringStr;

.m

+ (BOOL)checkStringWithPattern:(NSString*)patternStrstringStr:(NSString*)stringStr {

    NSString*pattern = patternStr;

    NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES%@",pattern];

    BOOLisMatch = [predevaluateWithObject:stringStr];

    returnisMatch;

}

上一篇 下一篇

猜你喜欢

热点阅读