Skip to content

Instantly share code, notes, and snippets.

@hebertialmeida
Created November 20, 2014 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hebertialmeida/be0b689bf0370d5a2764 to your computer and use it in GitHub Desktop.
Save hebertialmeida/be0b689bf0370d5a2764 to your computer and use it in GitHub Desktop.
Given a dictionary of words, return an array of the words whose match. (i.e. pattern "c.t" match with "cat", "cut", etc. because the dot notation stand for ANY character). Swift implementation.
import UIKit
var words = ["Cat", "Cot", "Brazil", "Cut", "cat", "Apple", "Watch"]
var filter = [String]()
for word in words {
if let match = word.rangeOfString("(C|c)(.*)t", options: .RegularExpressionSearch) {
filter.append(word)
}
}
println(filter)
//returns [Cat, Cot, Cut, cat]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment