Last active
March 29, 2021 07:19
-
-
Save yycking/1b3e6ee2ab8d355dabba9e9801422c10 to your computer and use it in GitHub Desktop.
~= operator for String
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
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