我有这样的产品的一系列成分:
text = 'Pork and beef, water, salt (1,7%), spices (white pepper, nutmeg, coriander, cardamom), stabilizer (E450), glucose, antioxidant (E316), a preservative (E250), flavorings'
我想检测其中的所有文本(成分),使其看起来像这样。
ingredientsList= ['Pork and beef', 'salt', 'spices', 'white pepper', 'nutmeg', 'coriander', 'cardamom', 'stabilizer', 'glucose', 'antioxidant', 'preservative', 'flavorings']
我在这里使用的当前正则表达式如下:
ingredients = re.findall(r'\([^()]*\)|([^\W\d]+(?:\s+[^\W\d]+)*)', text)
但它没有提供括号中的文本。我只是不想包含代码和百分比,但希望将所有成分都放在括号内。我应该在这里做什么?提前致谢。
最佳答案
您可以限制第一个分支仅匹配以 E
开头且后跟数字的代码:
\(E\d+\)|([^\W\d]+(?:\s+[^\W\d]+)*)
请参阅regex demo
现在,\(E\d+\)
将仅匹配类似 (Exxx)
的子字符串,其他子字符串将被处理。您也可以在此处添加百分比,以明确跳过它们 - \((?:E\d+|\d+(?:[.,]\d+)?%)\)
.
import re
rx = r"\(E\d+\)|([^\W\d]+(?:\s+[^\W\d]+)*)"
s = "Pork and beef, water, salt (1,7%), spices (white pepper, nutmeg, coriander, cardamom), stabilizer (E450), glucose, antioxidant (E316), a preservative (E250), flavorings"
res = [x for x in re.findall(rx, s) if x]
print(res)
关于python - 在Python中为复杂字符串设置正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40259185/