如何使用regex循环过滤特定字符前后的句子部分

2024-04-28 11:09:41 发布

您现在位置:Python中文网/ 问答频道 /正文

'我想在&;之前提取文本;在“:”和“|”之后使用regex并将其分为speaker和title

“有很多这样的句子,所以我需要写一个循环”

 text1='If I controlled the internet | Rives '
 text2='Life at 30,000 feet | Richard Brandson'
 text3='larry brilliant : A surprising idea for "solving" climate change'

Tags: the文本iftitleinternetatregex句子
3条回答

您可以使用这个简单的regex '.[:|].'

import re
text1='If I controlled the internet | Rives '
text2='Life at 30,000 feet | Richard Brandson'
text3='larry brilliant : A surprising idea for "solving" climate change'

text = (text1, text2, text3)

for item in text:
    title, speaker = re.split('.[:|].', item)
    print('title:', title, ' - Speaker:', speaker)

输出:

title: If I controlled the internet  - Speaker: Rives 
title: Life at 30,000 feet  - Speaker: Richard Brandson
title: larry brilliant  - Speaker: A surprising idea for "solving" climate change

注意最后一个:)

如果您愿意使用纯字符串函数而不是regex:

if '|' in text:
    title, speaker = text.split('|', 1)
elif ':' in text:
    speaker, title = text.split(':', 1)

使用正则表达式

re.compile('[\s]*[|:][\s]*').split(text)

相关问题 更多 >