As a modern programming language, Swift learns a great deal from other languages. But one thing disappoints us - there is no support for regular expression in language level in Swift.

Of course, you could say development for apps is not similar using Perl or Ruby and handling a lot of text match. Yes, indeed Cocoa developers are not living on regular expressions. But you should never deny the value of using the regular expression. Is it possible for us to use something like =~ to express a match with a regular expression?

Thanks to customizable operators in Swift, we could define =~ as a new operator. In Cocoa, NSRegularExpression class takes the charge of matching text. So it should not be hard to write a wrapper and let new defined =~ symbol operating on it. Both of the operands will be String since we want to play match in text. Similar to other languages, we could use the right operand to create an NSRegularExpression object, then use it and find matches for the left. First, we write a wrapper for NSRegularExpression like this:

struct RegexHelper {
    let regex: NSRegularExpression?

    init(_ pattern: String) {
        var error: NSError?
        regex = NSRegularExpression(pattern: pattern,
            options: .CaseInsensitive,
            error: &error)
    }

    func match(input: String) -> Bool {
        if let matches = regex?.matchesInString(input,
            options: nil,
            range: NSMakeRange(0, count(input))) {
            return matches.count > 0
        } else {
            return false
        }
    }
}

When using it, say we want to match a mail address, do like this:

let mailPattern =
    "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
let matcher = RegexHelper(mailPattern)
let maybeMailAddress = "onev@onevcat.com"

if matcher.match(maybeMailAddress) {
    print("A valid mail address")
}
// Output:
// A valid mail address

If you wonder what the long expression in mailPattern means...Uhh..I am sorry but it is not a lesson for regular expression. About this I suggest taking a look at this great interactive tutorial, If you have no luck to spare some time for it, see this post and get ready to copy & paste..

P.S. I stole mailPattern from this site as well.

Here we have a nice wrapper now. Next let us implement =~. For more about how to adapt an operator, please refer the Operator tip.

infix operator =~ {
    associativity none
    precedence 130
}

func =~(lhs: String, rhs: String) -> Bool {
    return RegexHelper(rhs).match(lhs)
}

With all of these, we now could use a neat syntax to use regular expression:

if "onev@onevcat.com" =~
    "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$" {
    print("A valid mail address")
}
// Output:
// A valid mail address

Swift 1.x will be focused on app development. I guess Apple and Chris will not stop here. They will take Swift to a new level and a wider stage. At that time, there might be support in language or standard library level. If that happens, this tip would become meaningless. However, I personally hope that day could come soon.