Finding Multiple Integers Inside A String Using Regex
I currently have this code below: Pattern intsOnly = Pattern.compile('\\d+'); Matcher matcher = intsOnly.matcher(o1.getIngredients()); matcher.find(); String inputI
Solution 1:
In your posted code:
matcher.find();
String inputInt = matcher.group();
You are matching the whole string with a single call to find. And then assigning the first match of digits to your String inputInt
. So for example, if you have the below string data, your return will only be 1
.
1 egg, 2 bacon rashers, 3 potatoes
You should use a while
loop to loop over your matches.
Pattern intsOnly = Pattern.compile("\\d+");
Matcher matcher = intsOnly.matcher(o1.getIngredients());
while (matcher.find()) {
System.out.println(matcher.group());
}
Post a Comment for "Finding Multiple Integers Inside A String Using Regex"