I have a list of strings, below is just one example:
str_list = ['Navitas', 'Organic Cacao Powder', '227g', 'SALE $7.49 ea', 'Regular $9:99-ea', 'Valid 01/12 - 01/18']
I want to extract the '227g' element based on finding the index.
unit_index = [idx for idx, val in enumerate(str_list) if val.endswith((' ml','-pack','{\d}g', ' L'))]
But this doesn't seem to work. I would like to find out where it ends with a digit followed by a unit of measure (ml, g, L)
CodePudding user response:
You can use \d \s?(ml|-pack|g|L)$ to check for a number with a unit after it.
import re
str_list = ['Navitas', 'Organic Cacao Powder', '227g', 'SALE $7.49 ea', 'Regular $9:99-ea', 'Valid 01/12 - 01/18']
r = r"\d \s?(ml|-pack|g|L)$"
unit_index = [idx for idx, val in enumerate(str_list) if re.search(r, val)][0]
