Skip to content

Instantly share code, notes, and snippets.

@eunjin3786
Created August 12, 2022 07:53
Show Gist options
  • Save eunjin3786/c9d3c1d334ec34ac1715fce67d02332b to your computer and use it in GitHub Desktop.
Save eunjin3786/c9d3c1d334ec34ac1715fce67d02332b to your computer and use it in GitHub Desktop.
import RegexBuilder
let regex = Regex {
"Hi, WWDC"
Repeat(.digit, count: 2)
"!"
}
let input = "Hi, WWDC21! Hi, WWDC22! Hi Hello"
let firstMatch: Regex.Match? = input.firstMatch(of: regex)
print("First Matched: \(firstMatch?.output)")
// First Matched: Optional("Hi, WWDC21!")
let wholeMatch: Regex.Match? = input.wholeMatch(of: regex)
print("Whole Matched: \(wholeMatch?.output)")
// Whole Matched: nil
let matches: [Regex.Match] = input.matches(of: regex)
print("Matched: \(matches.map { $0.output })")
// Matched: ["Hi, WWDC21!", "Hi, WWDC22!"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment