Custom Regex Components in Swift
The Missing Piece in Your Regex Toolkit
In a previous article we have looked at how powerful the Swift regex system is. We have gone through all the options from hardcoded regex strings to regex literals and finally the regex builder introduced in Swift 5.7. In this article we are going to look at another clever way for working with complex regular expressions, the RegexComponent
.
This often-overlooked feature provides a modular approach to regex construction, allowing developers to encapsulate complex patterns into reusable, composable components. As applications grow in complexity, maintaining and debugging regular expressions can quickly become challenging. This is where RegexComponent
shines by offering a more structured and maintainable solution. Whether you're parsing structured text, validating user input, or extracting information from documents, understanding how to leverage RegexComponent
can significantly enhance your Swift code's readability and flexibility.
What is a regex component?
This of a regex component as of any component in a programming context. It encapsulates certain parts of the logic for reusability. RegextComponent
s are the perfect tool for applications that need to use the same patter over and over again.
Take the following example:
let regex = Regex {
OneOrMore {
CharacterClass(
.anyOf("-."),
.word,
.digit
)
}
"@"
OneOrMore {
CharacterClass(
.anyOf("-"),
.word
)
}
"."
ChoiceOf {
"com"
"de"
}
}
This regex is meant for matching email addresses.
💡 Note: This article is not about email matching patters, thus the presented pattern may not be perfect but it can present the main concept of this article well
Now, imagine that your application requires you to use this pattern over and over again. You can find a way to share the regex variable accross your codebase but what if you need to encapsulate it in some other patterns? Like you need to find all rows that have the following format: [name]: [email]
In this case, your regex variable will not be much of a help, and that is where the RegexComponent
comes into play. Creating such a component is very simple. We just have to conform the RegexComponent
protocol and implment the only requirement which is var regex: Regex
Like this:
struct EmailPattern: RegexComponent {
var regex: Regex<Substring> {
Regex {
OneOrMore {
CharacterClass(
.anyOf("-."),
.word,
.digit
)
}
"@"
OneOrMore {
CharacterClass(
.anyOf("-"),
.word
)
}
"."
ChoiceOf {
"com"
"de"
}
}
}
}
After this, we can use our email component in other regular expressions like this:
let regex = Regex {
"Email: "
EmailComponent()
}
Related articles
Here are some more articles that may interest you. Check them out!
SwiftUI Transitions
published on September 7, 2025
SwiftSwiftUIAnimationsLearn how to create smooth, engaging UI animations in SwiftUI using built-in and custom transitions. From simple fades to complex combinations, this guide covers the essentials with best practices and examples.
Read moreSwiftUI Animation techniques
published on September 3, 2025
SwiftUISwiftAnimationsOne of the main reasons iOS feels so good to use is its fluent animations. Looking at successful apps, you would many times find these subtle but powerful animations. In this article, we are exploring the different animation APIs SwiftUI provides us and by the end of it, we will see how to recreate those beautiful visual effects we all love.
Read moreRegular expressions in Swift
published on February 27, 2025
SwiftSwift 5.7 transforms the regex experience from a developer's nightmare into an elegant solution, introducing compile-time validation and a SwiftUI-like DSL that makes even complex pattern matching readable, type-safe, and dramatically more approachable.
Read more