mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.8 KiB
Swift
53 lines
1.8 KiB
Swift
4 years ago
|
//
|
||
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
|
||
|
@objc
|
||
|
public extension NSRegularExpression {
|
||
|
|
||
|
@objc
|
||
4 years ago
|
func hasMatch(input: String) -> Bool {
|
||
4 years ago
|
return self.firstMatch(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) != nil
|
||
|
}
|
||
|
|
||
|
@objc
|
||
4 years ago
|
class func parseFirstMatch(pattern: String,
|
||
4 years ago
|
text: String,
|
||
|
options: NSRegularExpression.Options = []) -> String? {
|
||
|
do {
|
||
|
let regex = try NSRegularExpression(pattern: pattern, options: options)
|
||
|
guard let match = regex.firstMatch(in: text,
|
||
|
options: [],
|
||
|
range: NSRange(location: 0, length: text.utf16.count)) else {
|
||
|
return nil
|
||
|
}
|
||
|
let matchRange = match.range(at: 1)
|
||
|
guard let textRange = Range(matchRange, in: text) else {
|
||
|
return nil
|
||
|
}
|
||
|
let substring = String(text[textRange])
|
||
|
return substring
|
||
|
} catch {
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@objc
|
||
4 years ago
|
func parseFirstMatch(inText text: String,
|
||
4 years ago
|
options: NSRegularExpression.Options = []) -> String? {
|
||
|
guard let match = self.firstMatch(in: text,
|
||
|
options: [],
|
||
|
range: NSRange(location: 0, length: text.utf16.count)) else {
|
||
|
return nil
|
||
|
}
|
||
|
let matchRange = match.range(at: 1)
|
||
|
guard let textRange = Range(matchRange, in: text) else {
|
||
|
return nil
|
||
|
}
|
||
|
let substring = String(text[textRange])
|
||
|
return substring
|
||
|
}
|
||
|
}
|