swift -> 常用正则表达式--判断输入类型
转自 : http://blog.csdn.net/h643342713/article/details/54292935
//只能为中文 func onlyInputChineseCharacters(_ string: String) -> Bool { var inputString = "[\u{4e00}-\u{9fa5}]+" var predicate = NSPredicate(format: "SELF MATCHES %@", inputString) var Chinese = predicate.evaluate(withObject: string) return Chinese } //只能为数字 func onlyInputTheNumber(_ string: String) -> Bool { var numString = "[0-9]*" var predicate = NSPredicate(format: "SELF MATCHES %@", numString) var number = predicate.evaluate(withObject: string) return number } //只能为小写 func onlyInputLowercaseLetter(_ string: String) -> Bool { var regex = "[a-z]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var letter = predicate.evaluate(withObject: string) return letter } //只能为大写 func onlyInputACapital(_ string: String) -> Bool { var regex = "[A-Z]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var capital = predicate.evaluate(withObject: string) return capital } //允许大小写 func inputCapitalAndLowercaseLetter(_ string: String) -> Bool { var regex = "[a-zA-Z]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许大小写或数字(不限字数) func inputLettersOrNumbers(_ string: String) -> Bool { var regex = "[a-zA-Z0-9]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许大小写或数字(限字数) func inputNumberOrLetters(_ name: String) -> Bool { var userNameRegex = "^[A-Za-z0-9]{6,20}+$" var userNamePredicate = NSPredicate(format: "SELF MATCHES %@", userNameRegex) var inputString = userNamePredicate.evaluate(withObject: name) return inputString } //允许汉字,大小写或数字(限字数) func inputChineseOrLettersNumberslimit(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}]+[A-Za-z0-9]{6,20}+$" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许汉字或数字(不限字数) func inputChineseOrNumbers(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}]+[0-9]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许汉字或数字(限字数) func inputChineseOrNumbersLimit(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}][0-9]{6,20}+$" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许汉字,大小写或数字(不限字数) func inputChineseOrLettersAndNumbersNum(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}]+[A-Za-z0-9]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许汉字,大小写或数字(限字数) func inputChineseOrLettersNumberslimit(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}]+[A-Za-z0-9]{6,20}+$" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString }
相关推荐
flyingssky 2020-08-18
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26