//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄
/**
注意点: 1.看 GIF 效果图.
2.看连线视图的效果图.
3.看实现代码(直接复制实现效果).
*/
一、GIF 效果图:
二、连线视图的效果图:
图1:
图2:
三、实现代码:
=========================
===================================================
控制器1:ViewController.m
=========================
=========================================
==========
// ViewController.m
//正则表达式dome
//
// Created by石虎on 2017/8/11.
// Copyright © 2017年shihu. All rights reserved.
//
#import"ViewController.h"
#import"SHRegexPatternTool.h"
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//1.类方法
NSLog(@"我是数字--类方法-->%d",[SHRegexPatternToolvalidateBankCardNumber:@"6214830115447426"]);
NSLog(@"我是邮箱--类方法-->%d",[SHRegexPatternToolvalidateEmail:@"163@qq.com"]);
NSLog(@"我是汉字中文--类方法-->%d",[SHRegexPatternToolvalidateCustomRegex:@"^[\u4E00-\u9FA5]*$"TargetString:@"我只是中文"]);
//2.实例方法
SHRegexPatternTool*tool = [[SHRegexPatternToolalloc]init];
NSLog(@"我是数字--实例方法-->%d",[toolvalidateIsValidQQ:@"1383478"]);
NSLog(@"我是邮箱--实例方法-->%d",[toolvalidateIsValidMailbox:@"163@qq.com"]);
NSLog(@"我是汉字中文--实例方法-->%d",[toolvalidateCustomRegex:@"^[\u4E00-\u9FA5]*$"TargetString:@"我只是中文"]);
}
@end
控制器2:SHRegexPatternTool.h
=========================
========================================
===========
// SHRegexPatternTool.h
//正则表达式dome
//
// Created by石虎on 2017/8/11.
// Copyright © 2017年shihu. All rights reserved.
//
#import
@interfaceSHRegexPatternTool :NSObject
//============================ 实例化方法====================================//
/**
*验证字符串是否为合法的邮箱email
*
* @param targetString目标字符串
*
* @return YES/NO
*/
-(BOOL)validateIsValidMailbox:(NSString*)targetString;
/**
*验证QQ号
*
* @param targetString目标字符串
*
* @return YES/NO
*/
-(BOOL)validateIsValidQQ:(NSString*)targetString;
/**
*实例方法自定义正则验证
*
* @param customRegex自定义的正则表达式
* @param targetString目标字符串
*
* @return YES/NO
*/
- (BOOL)validateCustomRegex:(NSString*)customRegex TargetString:(NSString*)targetString;
//================================类方法==================================//
/**
*验证邮箱
*
* @param email邮箱
*
* @return YES/NO
*/
+ (BOOL)validateEmail:(NSString*)email;
/**
*验证银行卡号
*
* @param bankCardNumber银行卡号
*
* @return YES/NO
*/
+ (BOOL)validateBankCardNumber:(NSString*)bankCardNumber;
/**
*类方法自定义正则验证
*
* @param customRegex自定义的正则表达式
* @param targetString目标字符串
*
* @return YES/NO
*/
+ (BOOL)validateCustomRegex:(NSString*)customRegex TargetString:(NSString*)targetString;
控制器2:SHRegexPatternTool.m
========================
=========================================
==========
// SHRegexPatternTool.m
//正则表达式dome
//
// Created by石虎on 2017/8/11.
// Copyright © 2017年shihu. All rights reserved.
//
#import"SHRegexPatternTool.h"
@implementationSHRegexPatternTool
//============================ 实例化方法====================================//
//验证字符串是否为有效的邮箱email
-(BOOL)validateIsValidMailbox:(NSString*)targetString{
//NSString *regex = @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
NSString*regex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
return[selfregexPatternResultWithRegex:regexTargetString:targetString];
}
//验证QQ号
-(BOOL)validateIsValidQQ:(NSString*)targetString{
NSString*regex =@"^[1-9]*[1-9][0-9]*$";
return[selfregexPatternResultWithRegex:regexTargetString:targetString];
}
//实例方法自定义正则验证
- (BOOL)validateCustomRegex:(NSString*)customRegex TargetString:(NSString*)targetString{
return[selfregexPatternResultWithRegex:customRegexTargetString:targetString];
}
//====================================类方法========================================//
//邮箱
+ (BOOL)validateEmail:(NSString*)email
{
NSString*emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate*emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];
return[emailTestevaluateWithObject:email];
}
//银行卡
+ (BOOL)validateBankCardNumber: (NSString*)bankCardNumber
{
BOOLflag;
if(bankCardNumber.length<=0) {
flag =NO;
returnflag;
}
NSString*regex2 =@"^(\\d{15,30})";
NSPredicate*bankCardPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex2];
return[bankCardPredicateevaluateWithObject:bankCardNumber];
}
//类方法自定义正则验证
+ (BOOL)validateCustomRegex:(NSString*)customRegex TargetString:(NSString*)targetString{
NSPredicate*predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",customRegex];
return[predicateevaluateWithObject:targetString];
}
@end
谢谢!!!