[Pitch] Regular Expression Literals

I really like this comment by Chris Lattner in an old thread about adding regexes:

Just to throw some more ideas into the mix:

  • regex pattern matching is the dual of printing/formatting. I see them as very similar to string interpolation in a lot of ways. Just like interpolation, matching should be type driven (types should specify their matching rules) and there should be some way to customize formatting (e.g. the equivalent of printf style modifiers).
  • regex matching in Swift should integrate with pattern matching in general.
  • Perl 6 has some really great things in this department. That community has spent a very very large amount of time thinking about regex's. perl6 is not taking off in a huge way as a general language, but it makes sense to look at the things they are really great at and learn from them.

Plain regexes are inherently very not Swifty. It may make sense to add them as a legacy convenience, but I think the first approach should be to try to intelligently redesign regexes to create the best balance of familiarity, ease of use, and integration with Swift's features and ethos.

4 Likes