如何根据条件从字符串的开头删除文本?

How to remove text from the beginning of a string based on condition?

假设我有以下文本:

'Reuters - Life is beautiful.'
'agency.com - China\'s currency remains pegged to the dollar and the US currency\'s sharp falls in recent months have therefore made - Chinese export prices highly competitive.'
'AP - The number of days that beaches closed or posted warnings because of pollution rose sharply in 2003 due to more rainfall, increased monitoring and tougher -standards, an environmental group said on Thursday.'
'CNN - Warming water temperatures - in the central equatorial Pacific last month may indicate the start of a new El Nino.'

我只想删除 "-" 之前文本开头的所有文本,并且仅当 "-" 后跟一个空格时才删除。

我想要这样的东西:

if line.startswith(code_to_match_my_condition):
      strip_matched_text_from_line

所以结果是:

'Life is beautiful.'
'China\'s currency remains pegged to the dollar and the US currency\'s sharp falls in recent months have therefore made - Chinese export prices highly competitive.'
'The number of days that beaches closed or posted warnings because of pollution rose sharply in 2003 due to more rainfall, increased monitoring and tougher -standards, an environmental group said on Thursday.'
'Warming water temperatures - in the central equatorial Pacific last month may indicate the start of a new El Nino.'

我真的不知道如何编写代码。如果对此有任何帮助,我将不胜感激。

非常感谢您

用于搜索和删除块的简单易读代码:

if " - " in line:
    index = line.find(" - ")
    line = line[index+3:]

您可能需要使用正则表达式,例如

^[^-]+-\s+

并将其替换为空字符串,请参阅 a demo on regex101.com


Python 这可能是:

import re

strings = ['Reuters - Life is beautiful.',
           'agency.com - China\'s currency remains pegged to the dollar and the US currency\'s sharp falls in recent months have therefore made - Chinese export prices highly competitive.',
           'AP - The number of days that beaches closed or posted warnings because of pollution rose sharply in 2003 due to more rainfall, increased monitoring and tougher -standards, an environmental group said on Thursday.',
           'CNN - Warming water temperatures - in the central equatorial Pacific last month may indicate the start of a new El Nino.']

rx = re.compile(r'^[^-]+-\s+')

strings = list(map(lambda string: rx.sub("", string), strings))
print(strings)

并产生

['Life is beautiful.', "China's currency remains pegged to the dollar and the US currency's sharp falls in recent months have therefore made - Chinese export prices highly competitive.", 'The number of days that beaches closed or posted warnings because of pollution rose sharply in 2003 due to more rainfall, increased monitoring and tougher -standards, an environmental group said on Thursday.', 'Warming water temperatures - in the central equatorial Pacific last month may indicate the start of a new El Nino.']