java中的matcher没有产生所需的输出

时间:2015-07-14 08:57:05

标签: java regex

nodenName = VAS_DEL_SDC_LB1_ONM_DEL_10.200.98.74;
private String extractNodeName(String nodeName) {
        String output = "";
        Matcher match = Pattern.compile("[0-9]+").matcher(nodeName);
        while (match.find()) {
            output.split(match.group());
        }
        return output;
    }

任何人都可以帮我提取VAS_DEL_SDC_LB1_ONM_DEL,上面我试过它不起作用

3 个答案:

答案 0 :(得分:1)

将所有字符匹配到数字之前存在的第一个_

String output = "";
Matcher match = Pattern.compile(".*?(?=_[0-9])").matcher(nodeName);
while(match.find())
{
  output = m.group();
}
return output;

答案 1 :(得分:1)

private String extractNodeName(String nodeName) {
        String output = "";
        Matcher match = Pattern.compile("\\w+(?<!\\d|_)").matcher(nodeName);
        while (match.find()) {
            output = match.group();
        }
        return output;
}

结果: VAS_DEL_SDC_LB1_ONM_DEL

答案 2 :(得分:0)

试试这个:

private static String extractNodeName(String nodeName) {
    return nodeName.replaceAll("[\\d_.]{2,}", "");
}