New Swift's Regex DSL

Hey,

I love Regex, letting my kids slaming the keyboard until finding the good regex to do the job has always been a source of joy and pleasure.

But it seems that this time is over… at least for the Swift folks around here.

This DSL allows to create a Regex with in a “a la” SwiftUI approach.

import RegexBuilder

let emailPattern = Regex {
  let word = OneOrMore(.word)
  Capture {
    ZeroOrMore {
      word
      "."
    }
    word
  }
  "@"
  Capture {
    word
    OneOrMore {
      "."
      word
    }
  }
} // => Regex<(Substring, Substring, Substring)>

let email = "My email is my.name@mail.swift.org."
if let match = try emailPattern.firstMatch(in: email) {
  let (wholeMatch, name, domain) = match.output
  // wholeMatch: "my.name@mail.swift.org"
  //       name: "my.name"
  //     domain: "mail.swift.org"
}

The first exemple is absolutely terrific and I’ve watched the whole video. It’s promising.

What do you think of this new approach?
I’d be very happy to see it adopted in other languages. IMHO it’s time to drop the old syntax.

1 Like

Corresponding tweet for this thread:

Share link for this tweet.

1 Like

Interesting! Wonder if something similar could be done for other languages?

I’d be lost without rubular.com :lol: