[swift-evolution] Empower String type with regular expression

Chris Lattner clattner at apple.com
Tue Dec 8 23:29:23 CST 2015


> On Dec 8, 2015, at 12:14 PM, Jerome Paschoud via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I would like to see the String type to support regular expression per default. I think that a language that advertise itself as being a good scripting language should provide in its default implementation an easy way (=~ for example in Perl) to use regular expressions. I know that one can use the NSRegularExpression, but who really what to first create an NSRegularExpression object(whit all the nice escaping operation that come with every \), then get a NSTextCheckingResult, then get a range (and what I mean is a NSRange and not a NSRange<String.Index>) and finally perform slicing of your original string. 

Just MHO, but I’d really really like to see proper regex support in Swift someday.

I think it could fit naturally into the pattern matching syntax we already have - the obvious syntax for this pattern would use // delimiters. 

It is also probably worth burning first-class language support for regexes.  This would allow specifying variable captures inline in the pattern, would allow flexible syntax for defining regexes, support powerful extensions to the base regex model (e.g. Perl 6 style), and would provide better compile-time checking and error recovery for mistakes.

-Chris


More information about the swift-evolution mailing list