gpt4 book ai didi

python - Python 3 中的正则表达式 : match everything after a number or optional period but before an optional comma

转载 作者:行者123 更新时间:2023-11-28 16:25:07 28 4
gpt4 key购买 nike

我试图在没有任何测量值或说明的情况下退回食谱中的成分。成分列表如下所示:

['1  medium tomato, cut into 8 wedges',
'4 c. torn mixed salad greens',
'1/2 small red onion, sliced and separated into rings',
'1/4 small cucumber, sliced',
'1/4 c. sliced pitted ripe olives',
'2 Tbsp. reduced-calorie Italian salad dressing',
'2 Tbsp. lemon juice',
'1 Tbsp. water',
'1/2 tsp. dried mint, crushed',
'1/4 c. crumbled Feta cheese or 2 Tbsp. crumbled Blue cheese']

我想返回以下列表:

['medium tomato',
'torn mixed salad greens',
'small red onion',
'small cucumber',
'sliced pitted ripe olives',
'reduced-calorie Italian salad dressing',
'lemon juice',
'water',
'dried mint',
'crumbled Blue cheese']

我发现的最接近的模式是:

pattern = '[\s\d\.]* ([^\,]+).*'

但在测试中:

for ing in ingredients:
print(re.findall(pattern, ing))

每个测量缩写后的句点也被返回,例如:

['c. torn mixed salad greens']

同时

pattern = '(?<=\. )[^.]*$'

无法捕获没有句点的实例,如果两者都出现则捕获逗号,即:

[]
['torn mixed salad greens']
[]
[]
['sliced pitted ripe olives']
['reduced-calorie Italian salad dressing']
['lemon juice']
['water']
['dried mint, crushed']
['crumbled Blue cheese']

提前致谢!

最佳答案

你可以使用这个模式:

for ing in ingredients:
print(re.search(r'[a-z][^.,]*(?![^,])(?i)', ing).group())

图案细节:

([a-z][^.,]*) # a substring that starts with a letter and that doesn't contain a period
# or a comma
(?![^,]) # not followed by a character that is not a comma
# (in other words, followed by a comma or the end of the string)
(?i) # make the pattern case insensitive

关于python - Python 3 中的正则表达式 : match everything after a number or optional period but before an optional comma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37311197/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com