Skip to content

Instantly share code, notes, and snippets.

@yycking
Last active March 29, 2021 07:19
Show Gist options
  • Save yycking/1b3e6ee2ab8d355dabba9e9801422c10 to your computer and use it in GitHub Desktop.
Save yycking/1b3e6ee2ab8d355dabba9e9801422c10 to your computer and use it in GitHub Desktop.
~= operator for String
import Cocoa
import Foundation
extension String {
static func ~= (lhs: String, rhs: String) -> Bool {
if lhs == rhs {
return true
}
if let range = rhs.range(of: lhs, options: .regularExpression) {
return rhs[range] == rhs
}
return false
}
}
let text = "v1/4342adb9-de1e-4b10-aa3d-0f4919f67d42/abc"
let abc = "v1/[0-9a-f-]{36}/abc"
switch text {
case abc:
print("abc")
default:
print("no")
}
print(abc ~= text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment