[英]How to add space before and after specific character using regex in Python?
[英]How do I filter a part of sentence before and after a specific character using regex an loop
'我想提取“:”和“ |”之前和之后的文本 使用正则表达式并将其分成演讲者和标题。
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'
如果您愿意使用纯字符串函数而不是正则表达式:
if '|' in text:
title, speaker = text.split('|', 1)
elif ':' in text:
speaker, title = text.split(':', 1)
使用正则表达式
re.compile('[\s]*[|:][\s]*').split(text)
您可以使用此简单的正则表达式'.[:|].'
,
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
注意最后一个:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.