title: How to implement smart searching of contacts by name and number by T9 in iOS?
body: <p>I am working on a contacts app and I was wondering if it's possible to search contacts like T9 dealers do (letters associated with each number also filters results). So if I press 2 key, it should search for number '2' as well as 'ABC'.</p>
  
  <p>Code I am trying :</p>
  
  <pre><code>let enteredChar = dialerTextField.text!
  
          var pattern = keysDictionary?[enteredChar.last]
  
          pattern = "[\(String(describing: pattern!.first)) - \(String(describing: pattern!.last))]"
          // pattern = "[m-o]" Regex
          do {
  
              let matches = items.filter({
  
                  (item : String) -&gt; Bool in
  
                  let stringMatch = item.range(of: pattern!, options: .regularExpression, range: nil, locale: nil)
  
                  return stringMatch != nil ? true : false
  
              })
  
              let predicate = CNContact.predicateForContacts(matchingName: pattern!)
  
              let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey]
  
              var contacts = [CNContact]()
              var message: String!
  
              let contactsStore = CNContactStore()
  
              do {
                  contacts = try contactsStore.unifiedContacts(matching: predicate, keysToFetch: keys as [CNKeyDescriptor])
  
                  if contacts.count == 0 {
                      message = "No contacts were found matching the given name."
                  }
              }
              catch {
                  message = "Unable to fetch contacts."
              }
  
  
              print("matches : ", contacts)
          }
          catch {
  
          }
  </code></pre>
  
  <p>using above code I can search for contact name if I change <code>pattern</code> to say "Rob" but it does not work with Regex <code>"[p-s]"</code>. I'd like to search with regex. How can this be done? Thank you.</p>