正则表达式使用

Java代码例子

取出字符串中第一段数字和第一段中文

Pattern getNumberPattern = Pattern.compile("\\D*(\\d+)\\D*");
Pattern getChinesesPattern = Pattern.compile("[^\u4E00-\u9FA5]*([\u4E00-\u9FA5]+)[^\u4E00-\u9FA5]*");
Matcher matcher = null;
//项目负责人及联系电话
String fzrdh = ((String)resultMap.get( "FZRDH"));
//区分人名与电话,人名与电话混合且无规律
if(fzrdh != null && !fzrdh.equals("")) {
    //取出字符串中第一段数字作为电话
    matcher = getNumberPattern.matcher(fzrdh);
    String chargerPhone = null;
    // 整体匹配
    if(matcher.matches()) {
        chargerPhone = matcher.group(1);
        target.setChargerPhone(chargerPhone);
    }

    //取出第一段中文作为项目负责人
    matcher = getChinesesPattern.matcher(fzrdh);
    String charger = null;
    if(matcher.matches()) {
        charger = matcher.group(1);
        target.setCharger(charger);
    }
}

判断字符串是否包含中文字符

String chineseCharacterPattern = "[\u4e00-\u9fa5]";  
Pattern pattern = Pattern.compile(chineseCharacterPattern);  
Matcher matcher = pattern.matcher(string);
// 部分匹配
return matcher.find();

判断字符串是否包含非中文字符

String chineseCharacterPattern = "[^\u4e00-\u9fa5]";  
Pattern pattern = Pattern.compile(chineseCharacterPattern);  
Matcher matcher = pattern.matcher(string);  
return matcher.find();

判断字符串是否是数字形式

String simpleNumericPattern = "-?[0-9]+(\\.[0-9]+)?";  
Pattern pattern = Pattern.compile(simpleNumericPattern);  
Matcher matcher = pattern.matcher(string);  
return matcher.matches();