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!
Regular 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 moreThe RawRepresentable protocol
published on February 15, 2025
SwiftIf you’ve ever used a Swift enum with a raw value, you may not even realise that you have already worked with the RawRepresentable protocol. But what is it and how can you make use of it? In this article we are going to investigate the RawRepresentable protocol and I’m going to show you some neat ways for using the protocol to our advantage.
Read moreProportional layout with SwiftUI
published on January 25, 2025
SwiftUISwiftStarting from iOS 16, SwiftUI gives us the option to build custom layouts that can control very precisely where each subview should be placed. We have the option to build layouts that can be used just as the VStack, HStack or any other built in Layout you may be familiar with from SwiftUI.
Read more