Skip to content

Instantly share code, notes, and snippets.

@shalyf
Created December 13, 2018 03:44
Show Gist options
  • Save shalyf/e91f90b9139f8ee4500a81f6cc776085 to your computer and use it in GitHub Desktop.
Save shalyf/e91f90b9139f8ee4500a81f6cc776085 to your computer and use it in GitHub Desktop.
使用NSPredicate进行正则匹配
extension String {
/// 检查字符串是否匹配正则
///
/// - Parameter pattern: 正则表达式
/// - Returns: true - 匹配
func matchPattern(_ pattern: String) -> Bool {
let predicate = NSPredicate(format: "SELF MATCHES %@", pattern)
return predicate.evaluate(with: self)
}
}
@shalyf
Copy link
Author

shalyf commented Dec 13, 2018

一些复杂的正则可能无法匹配,NSRegularExpression更加稳妥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment