用于显示一个或两个单词(包括字符串)的python-regex模式

问题描述 投票:0回答:1

我需要制作一个正则表达式函数,使其仅在单词Red之后得到一个或两个单词(而且我需要在pandas系列中显示它,但我已经弄清楚了)

例如,给出此数据集:

49124     Variety Cake Caramel Red Velvet Chocolate Carrot
49344                       Beef & Bean Red Chili Burritos
49588           Spray Red Honeysuckle Nectar Air Freshener
49599                                            Red Beans

预期的输出将是:

49124                                 Red Velvet Chocolate
49344                                   Red Chili Burritos
49588                               Red Honeysuckle Nectar
49599                                            Red Beans

除了模式,我已经弄清楚了一切,这就是我的方法:

def red_stuff():
  df = pd.read_csv(file_name)
  pattern = ' Red .*$'
  return df[df['product_name'].str.contains(pattern)]['product_name']

这只是需要调整的模式。其他一切都很好。关于如何实现正确模式的任何想法?

python regex
1个回答
0
投票

是的,可以花很多时间在正则表达式上,幸运的是有在线编辑器。看一下这个:https://regex101.com/r/4Hn6Vh/2

© www.soinside.com 2019 - 2024. All rights reserved.