diff --git a/Sources/_StringProcessing/Regex/Match.swift b/Sources/_StringProcessing/Regex/Match.swift index a86899041..b060da093 100644 --- a/Sources/_StringProcessing/Regex/Match.swift +++ b/Sources/_StringProcessing/Regex/Match.swift @@ -184,3 +184,15 @@ extension Substring { try? r.regex.prefixMatch(in: self) } } + +@available(SwiftStdlib 5.7, *) +public func ~=(regex: Regex, input: String) -> Bool { + guard let _ = try? regex.wholeMatch(in: input) else { return false } + return true +} + +@available(SwiftStdlib 5.7, *) +public func ~=(regex: Regex, input: Substring) -> Bool { + guard let _ = try? regex.wholeMatch(in: input) else { return false } + return true +} diff --git a/Tests/RegexTests/AlgorithmsTests.swift b/Tests/RegexTests/AlgorithmsTests.swift index a788ad13c..d19d6c43d 100644 --- a/Tests/RegexTests/AlgorithmsTests.swift +++ b/Tests/RegexTests/AlgorithmsTests.swift @@ -172,4 +172,19 @@ class RegexConsumerTests: XCTestCase { s2.matches(of: regex).map(\.0), ["aa"]) } + + func testSwitches() { + switch "abcde" { + case try! Regex("a.*f"): + XCTFail() + case try! Regex("abc"): + XCTFail() + + case try! Regex("a.*e"): + break // success + + default: + XCTFail() + } + } }