I have a Arraylist, like this:
ArrayList<String> moisturizersPrices = [Price: Rs. 365, Price: Rs. 299, Price: Rs. 12, Price: 220, Price: 95, Price: 216]
for that I am using following logic:
moisturizersPrices.replaceAll("[^0-9.]", "")
and it is returning me something like this:
[.365, .299, .12, 220, 95, 216]
Now I want to remove all characters from there, except for numeric, such as it should be giving me results like this:
[365, 299, 12, 220, 95, 216]
I need to know, where I am making mistake.
You could try a Pattern
match like
List<String> moisturizersPrices = List.of(
"Price: Rs. 365",
"Price: Rs. 299",
"Price: Rs. 12",
"Price: 220",
"Price: 95",
"Price: 216"
);
Pattern p = Pattern.compile("[0-9]+$");
List<Integer> numbers = moisturizersPrices.stream()
.map(p::matcher)
.filter(Matcher::find)
.map(m -> Integer.parseInt(m.group()))
.collect(Collectors.toList());
[365, 299, 12, 220, 95, 216]