大家好稻轨,本人總結(jié)了一些實用的正則表達(dá)式判斷方法励翼,經(jīng)測試可以用(僅針對iOS),如果還有其他常用的方法巡李,大家可以寫出來互相學(xué)習(xí)抚笔,謝謝思密達(dá)!
//匹配Email
- (BOOL)isEmail:(NSString*)str
{
NSString *regex = @"^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+(com|net|org|edu|gov|cn|info)$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
//匹配正數(shù)
- (BOOL)isNumber:(NSString*)str
{
NSString *regex = @"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
//匹配正整數(shù)
- (BOOL)isNumber1:(NSString*)str
{
NSString *regex = @"^[1-9]\\d*$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
//匹配正整數(shù)或0
- (BOOL)isNumber2:(NSString*)str
{
NSString *regex = @"^[0-9]\\d*$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
//匹配整數(shù)
- (BOOL)isNumber3:(NSString*)str
{
NSString *regex = @"^-?[1-9]\\d*$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
//匹配非負(fù)數(shù)
- (BOOL)isNumber4:(NSString*)str
{
NSString *regex = @"^\\d+(\\.\\d+)?$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
//匹配手機
- (BOOL)isMobile:(NSString*)str
{
NSString *regex = @"^(13[0-9]|15[0-9]|18[0-9]|14[0-9]]|17[0-9])\\d{8}$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
//匹配英文數(shù)字
- (BOOL)isEnglishAndNumber:(NSString*)str
{
NSString *regex = @"^[A-Za-z0-9]+$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
//匹配中文
- (BOOL)checkName:(NSString*)str{
NSString *regex = @"^[\u4E00-\u9FA5]+$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}
// 校驗密碼強度
//密碼的強度必須是包含大小寫字母和數(shù)字的組合侨拦,不能使用特殊字符殊橙,長度在8-10之間。
- (BOOL)checkPassword:(NSString*)str{
NSString *regex = @"^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:str];
}