Created
December 13, 2018 03:44
-
-
Save shalyf/e91f90b9139f8ee4500a81f6cc776085 to your computer and use it in GitHub Desktop.
使用NSPredicate进行正则匹配
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
/// 检查字符串是否匹配正则 | |
/// | |
/// - Parameter pattern: 正则表达式 | |
/// - Returns: true - 匹配 | |
func matchPattern(_ pattern: String) -> Bool { | |
let predicate = NSPredicate(format: "SELF MATCHES %@", pattern) | |
return predicate.evaluate(with: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一些复杂的正则可能无法匹配,NSRegularExpression更加稳妥