Regex Literals and Matching
Match patterns with the Regex type.
Regex Literals and Matching is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Regex in Swift
Swift 5.7+ has native regular expressions in the standard library and Foundation. You can write a regex as a literal between slashes, like /\d+/, and the compiler checks it at build time.
A Regex Literal
A regex literal produces a Regex value. Its type captures what the match yields. The simplest pattern matches a fixed sequence of characters.
let pattern = /hello/
if "hello world".contains(pattern) {
print("Found hello")
}wholeMatch(of:)
wholeMatch(of:) succeeds only if the entire string matches the pattern. It returns an optional match, or nil if the whole string does not match.
let digits = /\d+/
if let m = "12345".wholeMatch(of: digits) {
print("Whole match: \(m.output)")
} else {
print("No whole match")
}firstMatch(of:)
firstMatch(of:) scans the string and returns the first place the pattern matches anywhere inside it.
let word = /\w+/
if let m = " swift rocks".firstMatch(of: word) {
print("First word: \(m.output)")
}Accessing the Match Range
A match exposes a range into the original string, so you know exactly where it occurred.
let text = "abc123def"
if let m = text.firstMatch(of: /\d+/) {
print("Matched: \(m.output)")
print("At: \(text[m.range])")
}Character Classes
Inside a pattern, \d matches a digit, \w a word character, \s whitespace. Square brackets define a custom set like [aeiou].
let vowel = /[aeiou]/
if let m = "sky".firstMatch(of: vowel) {
print("Vowel: \(m.output)")
} else {
print("No vowels found")
}Quantifiers
Quantifiers control repetition: * is zero or more, + is one or more, ? is optional, and {n,m} is a bounded count.
let phone = /\d{3}-\d{4}/
print("555-1234".wholeMatch(of: phone) != nil)
print("12-34".wholeMatch(of: phone) != nil)Anchors
Use ^ to anchor at the start and $ at the end of a line. Anchors do not consume characters; they assert a position.
let startsWithCap = /^[A-Z]\w*/
if let m = "Swift".firstMatch(of: startsWithCap) {
print("Matched: \(m.output)")
}Case Insensitive Matching
You can ignore case by appending .ignoresCase() to a regex value, useful when the input casing is unpredictable.
let pattern = /swift/.ignoresCase()
print("I love SWIFT".contains(pattern))Matching with matches(of:)
matches(of:) returns a collection of every non-overlapping match in the string, letting you iterate over all of them.
let nums = "a1 b22 c333"
for m in nums.matches(of: /\d+/) {
print(m.output)
}Validating Input
A common use is validation: build a pattern for the full expected format and check with wholeMatch, which returns nil on a mismatch.
func isZip(_ s: String) -> Bool {
s.wholeMatch(of: /\d{5}/) != nil
}
print(isZip("90210"))
print(isZip("ABCDE"))Quick Check
Which method only succeeds when the entire string matches?
Recap
You learned to write regex literals with slashes and to match using wholeMatch(of:), firstMatch(of:), and matches(of:). You also saw character classes, quantifiers, anchors, and case-insensitive matching. Next you will pull data out of matches with capturing groups.
Frequently asked questions
Is the “Regex Literals and Matching” lesson free?
Yes — the full text of “Regex Literals and Matching” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Regex Literals and Matching”?
Match patterns with the Regex type. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Regex Literals and Matching” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Regex Literals and Matching
- Capturing Groups
- RegexBuilder DSL
- Replacing and Splitting Text