【发布时间】:2020-10-16 07:37:07
【问题描述】:
您好,我在这里发布我的问题,因为我不明白我的潜在错误。我正在解析成分列表。其中一些有诸如“* no-ogm”之类的指控。我成功地在字典中提取了大部分这些指控,以便直接从其他成分列表中提取已经引用的指控。问题是我的正则表达式适用于所有在线正则表达式测试器,但在我的 jupyter 笔记本中没有,我不明白为什么。这是一个例子:
string='''chickpeas* (31%), water, sesame oil* (11%), tofu* (soybeans*, water, gelling agent (nigari (magnesium chloride))), onions*, carrots*, yeast*, celery*, non-hydrogenated sunflower oil*, cashew nuts*, tomato paste*, vegetable bouillon* (sea salt, maize starch*, glucose syrup*, sunflower oil*, carrots*, onion*, parsnips*, turmeric*, ginger*, parsley*, nutmeg*, lovage*, bay leaves*, black pepper*), sea salt, ginger*, locust bean*, coriander*, turmeric*, cumin*, fenugreek*, nutmeg, black pepper*, cinnamon*, mustard seeds*, cardamom*, cayenne pepper* *from organic agriculture'''
和正则表达式:
pattern=re.findall('\*{1,3}\s{0,2}\bfrom organic agriculture\b\s{0,2}$',string)
在 Regex101 和 Pythex 中,可以清楚地找到字符串“*来自有机农业”的子集。在我的 jupyter 笔记本模式中返回“无”......为什么?我尝试了许多正则表达式标志来纠正这种行为,但是......没有任何效果。
这个问题在大规模范围内尤其成问题,因为我有一个如上所述的指控字典,我循环遍历我的每个字典键以在多个字符串中找到相应的模式。
提前感谢您的帮助
【问题讨论】: