I have got a String for example.
"Quantity : 2.85 kg"
Here I only need to extract the number with exact configuration 2.85
.
String.replaceAll("[^0-9]","");
only extracts number as 285
, but I need 2.85.
Kindly help.
I have got a String for example.
"Quantity : 2.85 kg"
Here I only need to extract the number with exact configuration 2.85
.
String.replaceAll("[^0-9]","");
only extracts number as 285
, but I need 2.85.
Kindly help.
You must be kidding guys, the pattern for a digit is
p = "/d*(./d+)?"
and extract it is :
Matcher m = Pattern.compile(p)
while(m.find())
res = m.group(0)
Regex is any character, then one or more digits with a dot and following two digits, space and kg. A capturing group is used to capture the part thas is to be extracted.
String test = "Quantity : 2.85 kg";
Pattern pattern = Pattern.compile(".*(\\d+\\.\\d\\d) kg");
Matcher matcher = pattern.matcher(test);
if(matcher.matches()){
System.out.println(matcher.group(1));
}else{
System.out.println("no match");
}