What’s new in Swift 5.7 | Regex Type & Regex Builder Api

The Blue Prototype
3 min readJun 19, 2022
Photo by Markus Spiske on Unsplash

Regex are very cool. Regexes, also known as regular expressions, are a powerful tool for matching patterns in text. Swift supports several ways to create a regular expression, including from a string, as a literal, and using this DSL.

‘Regex’ type, is a new type in the Swift Standard Library. A language built-in Regex literal syntax, which makes this powerful and familiar concept. And finally, a result builder API called RegexBuilder. It is a domain-specific language, or DSL.

Get Started

Here I’ll discuss about above 3 ways to create regex:

  1. Regex from String
  2. Regex as literal
  3. Regex by Builder api

Lets start with an example — I have a string which contains some hashtags. Now I’ll create regex to find all the hashtags from the string.

let text = "My #name is #sanjeev and I am an #engineer with #10 years of #experience. Currently, working on #apple #technology."

1. Regex from String

--

--

Responses (2)