JAVA get unit of measure and measurement from a string -
i have string:
string = "2 ltr. btl., select varieties when buy 6 $1.25 ea.-50¢ mix or match";
is possible extract unit of measurement given string? (2 ltr)
note: unit of measurement , measurement appear anywhere in string.
edit: 1 of these keywords should appear
oz. oz lbs. lbs lb. lb kg. kg k g. g pk. pk ea. ea ml. ml pck. pck ct. ct qt. qt liter ltr ltr. fl oz fl oz.
i unit of measurement , corresponding measurement.
the regex extract amount (with optional decimal part) , measure unit is:
(?x)\d+(?:\.\d+)?\s+ (?: (?:fl )?oz(?:\.|\b)|lbs?(?:\.|\b)|kg(?:\.|\b)|kg?\b|g(?:\.|\b) | pc?k(?:\.|\b)|ea(?:\.|\b)|ml(?:\.|\b)|[cq]t(?:\.|\b) | liter\b|ltr(?:\.|\b) )
demo: https://regex101.com/r/uz7yz6/4
corresponding java code:
string input = "2 ltr. btl., select varieties when buy 6 $1.25 ea.-50¢ mix or match"; pattern pattern = pattern.compile( "(?x)\\d+(?:\\.\\d+)?\\s+" + " (?:" + " (?:fl )?oz(?:\\.|\\b)|lbs?(?:\\.|\\b)|kg(?:\\.|\\b)|kg?\\b|g(?:\\.|\\b)" + " | pc?k(?:\\.|\\b)|ea(?:\\.|\\b)|ml(?:\\.|\\b)|[cq]t(?:\\.|\\b)" + " | liter\\b|ltr(?:\\.|\\b)" + " )" ); matcher matcher = pattern.matcher(input); while (matcher.find()) { system.out.println(matcher.group()); }
Comments
Post a Comment