Swift should provide native regular expression literals

Originator:subdigital
Number:rdar://17257306 Date Originated:10-Jun-2014 04:00 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Swift
Classification:Feature (New) Reproducible:Not Applicable
 
Summary:

Any modern language should natively support regular expression literals without resorting to constructing a class and interpolating a pattern into a string.  Of course a class equivalent would be driving the functionality and be helpful for more dynamic construction of patterns.  Regex literals can really clean up code and make it more readable.

There is not a single mention of Regular Expressions in the Swift book and while I know we still have NSRegularExpression, it is cumbersome to use.

Proposal:

A regular expression literal might look like the following:

let pattern = /[Cc]ats?/

A simple way of detecting a match:

if pattern.isMatch(text) {
    …
}

It would be great if this could be simplified to a special operator, such as =~ in Ruby.

Also enumerate over matches:

for match in pattern.matches(text) {
   println(“Found \(match.value) at character position: \(match.position)”)
}

Support for named groups:

let naiveEmailPattern = /(<?username>[A-Za-z-_0-9])@(<?company>[A-Za-z0-9_-]).corp/
for match in naiveEmailPattern.matches(text) {
	let username = match[“username”]
        let company = match[“company”]
        let email = match.value
        println(“The user \(username) works at \(company) and has an email address of \(email)”)
}

Comments

I dont know how this work(openradar...) can I like or set agree some place?

BUT its a serious problem that Swift are created without regular expression!

Please add it, Apple.

(It cant be the meaning of Swift(a moden language), that we should ever go back go use Objective-C code or any of the NextStep class names!)


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!