手机通讯录联系人搜索

1. 需求:
 
引用
手机通讯录联系人搜索支持单字,拼音跳跃搜索


2. 实现原理
 
引用
将搜索关键词按字符切分,组合成正则表达式,再从db中获得contactsAllList,遍历将field转换为拼音,类似"YAO 姚 YI 亦 RONG 容"的格式,转换后通过生成的正则表达式匹配,成功则代表搜索成功


3. 正则表达式
^(.*\\b)?$1.*$


4. 测试
static String sourceStr = "YAO 姚 YI 亦 RONG 容";

static String key = "YI";

static String reg = "(.*\\b)?$1";

// TODO 优化:charAt()效率,StringBuffer
public static void main(String[] args) {
    String regex = "";
    key = key.toUpperCase();
    for (int i = 0; i < key.length(); i++) {
        regex += reg.replace("$1", String.valueOf(key.charAt(i)));
    }
    regex = "^" + regex + ".*$";
    System.out.println(regex);
        System.out.println(Pattern.compile(regex).matcher(sourceStr).matches());
}

你可能感兴趣的:(正则表达式,搜索,通讯录)