[英]How do I filter a part of sentence before and after a specific character using regex an loop
'I want to extract the text before & after ":" and "|" '我想提取“:”和“ |”之前和之后的文本 using regex and seperate it into speaker and 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'
If you are willing to use plain string functions instead of regex: 如果您愿意使用纯字符串函数而不是正则表达式:
if '|' in text:
title, speaker = text.split('|', 1)
elif ':' in text:
speaker, title = text.split(':', 1)
使用正则表达式
re.compile('[\s]*[|:][\s]*').split(text)
you can use this simple 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)
output: 输出:
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
note the last one :) 注意最后一个:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.