-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.swift
More file actions
27 lines (24 loc) · 958 Bytes
/
StringExtensions.swift
File metadata and controls
27 lines (24 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//
// StringExtensions.swift
// BetterKit
//
// Created by Jangle's MacBook Pro on 8/14/18.
//
import Foundation
extension String {
func matches(pattern: String, options: NSRegularExpression.Options = [], matchingOptions: NSRegularExpression.MatchingOptions = []) -> Bool {
var expression: NSRegularExpression!
do {
expression = try NSRegularExpression(pattern: pattern, options: options)
} catch {
print("Invalid pattern: %@\n%@", pattern, error)
return false
}
return matches(expression, matchingOptions: matchingOptions)
}
func matches(_ input: NSRegularExpression?, matchingOptions: NSRegularExpression.MatchingOptions = []) -> Bool {
guard let input = input else { return false }
let range = NSRange(location: 0, length: self.count)
return input.firstMatch(in: self, options: matchingOptions, range: range)?.range == range
}
}