Python中文
首页
教程
问答
标签
搜索
登录
注册
python3中的Regex:匹配数字或可选句点之后但在可选comm之前的所有内容
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试着从配方中退回原料,没有任何测量或指导。成分列表如下:</p> <pre><code>['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'] </code></pre> <p>我想返回以下列表:</p> ^{pr2}$ <p>我发现的最接近的模式是:</p> <pre><code>pattern = '[\s\d\.]* ([^\,]+).*' </code></pre> <p>但在测试中:</p> <pre><code>for ing in ingredients: print(re.findall(pattern, ing)) </code></pre> <p>每个度量缩写后面的句点也会返回,例如:</p> <pre><code>['c. torn mixed salad greens'] </code></pre> <p>同时</p> <pre><code>pattern = '(?<=\. )[^.]*$' </code></pre> <p>无法捕获没有句点的实例,如果两者都出现,则捕获逗号,即:</p> <pre><code>[] ['torn mixed salad greens'] [] [] ['sliced pitted ripe olives'] ['reduced-calorie Italian salad dressing'] ['lemon juice'] ['water'] ['dried mint, crushed'] ['crumbled Blue cheese'] </code></pre> <p>提前谢谢你!在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您可以使用此模式:</p> <pre><code>for ing in ingredients: print(re.search(r'[a-z][^.,]*(?![^,])(?i)', ing).group()) </code></pre> <p>图案细节:</p> ^{pr2}$
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
plt.savefig不会覆盖现有文件
10 回答
plt.savefig不保存图像
3 回答
plt.savefig在jupyter笔记本中不起作用
2 回答
plt.savefig在从另一个fi调用时停止工作
1 回答
plt.savefig在调用plt.show之前保存空数字
5 回答
plt.save不创建png文件
6 回答
plt.scatter overlay分类数据帧列
6 回答
Plt.Scatter:如何添加title、xlabel和ylab
10 回答
plt.scatter()绘图与Matplotlib中的plt.plot()绘图类似
4 回答
plt.scatter错误'NoneType'对象在成功运行后没有属性'sqrt'
2 回答
plt.set_title()中的标题字符串有误
4 回答
plt.show()
2 回答
plt.show()不在Jupyter笔记本上渲染任何内容
6 回答
plt.show()不打印plt.plot only plt.scatter
7 回答
plt.show()不显示三维散射图像
1 回答
plt.show()不显示任何内容
1 回答
plt.show()不显示数据,而是保留它供下一个图表使用(spyder)
5 回答
plt.show()使终端挂起
5 回答
plt.show()无法使用此代码
8 回答
plt.show()没有打开新的图形风
7 回答