user3305374
user3305374

Reputation: 7

matcher in java not yielding desired output

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;
    }

can anybody help me out for extracting only VAS_DEL_SDC_LB1_ONM_DEL, the above i tried its not working

Upvotes: 0

Views: 53

Answers (3)

user4910279
user4910279

Reputation:

Try this:

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

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

Just match all the chars upto the first _ which exist before a digit.

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

Upvotes: 1

Kerwin
Kerwin

Reputation: 1212

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

Result: VAS_DEL_SDC_LB1_ONM_DEL

Upvotes: 1

Related Questions