Created
August 12, 2022 07:53
-
-
Save eunjin3786/c9d3c1d334ec34ac1715fce67d02332b to your computer and use it in GitHub Desktop.
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 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