小编tbj*_*rch的帖子

提取字符串第一部分的正则表达式

我有以下短语列表:

[
  'This is erleada comp. recub. con película 60 mg.',
  'This is auxina e-200 uicaps. blanda 200 mg.',
  'This is ephynalsol. iny. 100 mg.',
  'This is paracethamol 100 mg.'
]
Run Code Online (Sandbox Code Playgroud)

我需要得到以下结果:

[
  'This is erleada.',
  'This is auxina.',
  'This is ephynalsol.',
  'This is paracethamol.'
]
Run Code Online (Sandbox Code Playgroud)

我编写了以下函数来清理短语:

def clean(string):
    sub_strings = [".","iny","comp","uicaps]
    try:
        string = [string[:string.index(sub_str)].rstrip() for sub_str in sub_strings]
        return string
    except:
        return string
Run Code Online (Sandbox Code Playgroud)

并按如下方式使用它:

for phrase in phrases:
    drug = clean(phrase)
Run Code Online (Sandbox Code Playgroud)

python regex

1
推荐指数
1
解决办法
56
查看次数

标签 统计

python ×1

regex ×1