【问题标题】:Extract the word after a digit from the string [closed]从字符串中提取数字后的单词[关闭]
【发布时间】:2021-07-04 01:09:20
【问题描述】:

我有 1000 行字符串。我想根据python中的条件从字符串中提取单词。

例如:'1.雪碧 (9x)::=2。咖啡::= 奶油 (10x)::= 糖 (10x)::= 小::=3。一包黑糖 (6x)'

输出应为:

-> 雪碧、咖啡、黑糖包。

就像我想提取数字 1.,2.,3 之后的单词一样。并删除所有前后的特殊字符。

【问题讨论】:

    标签: python regex string nlp data-science


    【解决方案1】:

    我们可以在这里使用re.findall

    inp = '1. Sprite (9x)::=2. Coffee::= Cream (10x)::= Sugar (10x)::= Small::=3. Packet of Black Sugar (6x)'
    matches = re.findall(r'\d+\. (\w+(?: \w+)*)', inp)
    print(matches)  # ['Sprite', 'Coffee', 'Packet of Black Sugar']
    

    【讨论】:

    • @HAthedatascientist 我已经回答了你上面实际提出的问题。如果您有其他要求,此时应该提出一个新问题。
    猜你喜欢
    • 2023-02-13
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2014-11-06
    • 2011-11-29
    相关资源
    最近更新 更多