2017-12-07 14 views
0

電話番号を取得する方法ホーム、仕事、モバイル、ファックスなどのラベルを文字列から取得します。 たとえば、以下の文字列があり、番号の1つ、つまり(91)98203 88212がモバイルです。どのようにそれを検出するか。文字列から電話番号ラベルを取得

Phone: (91-22) 6641 1234 
Direct: (91-22) 6691 8972 
Fax: (91-22) 6691 1455 
Mobile: (91) 98203 88212 

名刺のOCRアプリの動作と似ています。電話番号ラベルを検出するためにどのようなロジックが使用されていますか?

+0

は多分正規表現を試してみてください? –

+0

@ N.Ivanov Regexは、有効な番号かどうかだけを通知します。ファックス、モバイル、ホームのようなタイプではありません。 – Nitesh

+2

@Nitesh情報を抽出するために正規表現を使用していますが、それは非常に一般的な使用です – Scriptable

答えて

-1

CNContactオブジェクトの場合は、これらのオブジェクトを簡単に取得できます。

for item in personContact.phoneNumbers { 
     if let label = item.label{ 
      let localizedLabel = CNLabeledValue<NSString>.localizedString(forLabel: label) 
//You will get your label here 
      print(localizedLabel) 
     }else{ 
      print(item.value.stringValue) 
     } 
    } 

それとも、文字列値のみを持っているならば、使用することができます。

let contacts = ["Phone: (91-22) 6641 1234", 
    "Direct: (91-22) 6691 8972", 
    "Fax: (91-22) 6691 1455", 
    "Mobile: (91) 98203 88212"] 
    for contact in contacts{ 
     let contactParts = contact.components(separatedBy: ":") 
     print(contactParts[0]) 
    } 
関連する問題