Reputation: 1
I'm trying to extract all numbers(integers and decimals) from a .txt file for a mobile recipe app(I'm using Android Studio with Java). I want to extract all numbers except numbers like 180°C, 190 °C, 6hours, 6 hours, 5minutes, 5 minutes(i.e. any number execpt ones with °C or hour/s minute/s and second/s suceeding the number with or without a space)
I would also like to extrat the word after the number match as a sepreate match, so for 3 eggs match 3 and eggs and for 10ml milk match 10 and milk but do not match 180°C and 6 minutes.
The goal of all this is to let the user enter the number of times the user wants to make the recipe, say 2 and then update all the ingrients too twice the original recipe, but leave the 180°C and say 30 minutes as obivously 360°C and 60 minutes is incorrect?
Thank you in advance
I got the ingredients list to work with:
.*?([\\d.]+).*
for the number and:
[0-9.]
for the ingredients as there is only 1 ingredient per line.
I'm struggling to get the "Method" part of the recipe to work(say when a recipe says use 2 of the 4 eggs in the Method). I have the following regex:
(?<!°C)(?<!hour)(?<!minute)(?<!minutes)\\b\\d+(\\.\\d+)?\\b(?!\\s*(°C|hour|minute|minutes))
it works the problem is it matches 10 in "10 ml milk" but not "10ml milk" and I have no idea how to get milk then to match?
Upvotes: 0
Views: 149
Reputation: 42
try using: "((\\d+?(/\\d+)?)\\s?(m?l|cups?|teaspoon|tablespoon)?)\\s(?![°C]|hours?|minutes?|seconds?)(\\b\\w+\\b)"
use this for detection decimals:
"((\d+?(\.\d+)?)\s?(m?l|cups?|teaspoon|tablespoon)?)\s(?![°C]|hours?|minutes?|seconds?)(\b\w+\b)"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Demo {
public static void main(String[] args) {
// use this for detecting decimals, but not fractions.
// Pattern pattern = Pattern.compile("((\\d+?(\\.\\d+)?)\\s?(m?l|cups?|teaspoon|tablespoon)?)\\s(?![°C]|hours?|minutes?|seconds?)(\\b\\w+\\b)");
Pattern pattern = Pattern.compile("((\\d+?(/\\d+)?)\\s?(m?l|cups?|teaspoon|tablespoon)?)\\s(?![°C]|hours?|minutes?|seconds?)(\\b\\w+\\b)");
Matcher matcher = pattern.matcher("3 eggs 1cup flour 1/2 cup sugar 1 hour 50 minutes 1/2teaspoon salt minutes of salt 30ml milk");
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
System.out.println("number match: " + matcher.group(2));
System.out.println("Cups/ml/teaspoon match: " + matcher.group(4));
System.out.println("ingredient match: " + matcher.group(5));
System.out.println();
}
}
}
Output:
Full match: 3 eggs
number match: 3
Cups/ml/teaspoon match: null
ingredient match: eggs
Full match: 1cup flour
number match: 1
Cups/ml/teaspoon match: cup
ingredient match: flour
Full match: 1/2 cup sugar
number match: 1/2
Cups/ml/teaspoon match: cup
ingredient match: sugar
Full match: 1/2teaspoon salt
number match: 1/2
Cups/ml/teaspoon match: teaspoon
ingredient match: salt
Full match: 30ml milk
number match: 30
Cups/ml/teaspoon match: ml
ingredient match: milk
Upvotes: 1