import re
text = """Ingredients: organic cane sugar, whole-wheat flour,
mono & diglycerides. Manufactured in a facility that uses nuts."""
# Search everything that comes after 'Ingredients: ' and before '.'
m = re.search('(?<=Ingredients: ).+?(?=\.)', text, re.DOTALL) # DOTALL: make . match newlines too
items = m.group(0).replace('\n', ' ').split(',') # Turn newlines into spaces, make a list of items separated by ','
items = [ i.strip() for i in items ] # Remove leading whitespace in each item
print items
这个Python代码给出了以下输出:
['organic cane sugar', 'whole-wheat flour', 'mono & diglycerides']
它要求配料在“配料:”后面,所有配料都要列在“.”之前,就像你的情况一样。在相关问题 更多 >
编程相关推荐