'我要提取“:”和“ |”之前和之后的文本使用正则表达式并将其分成演讲者和标题。
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'
答案 0 :(得分:2)
如果您愿意使用纯字符串函数代替正则表达式:
string(56) "40.76 -73.984 test 73.984 more test second line -73.984 "
答案 1 :(得分:0)
使用正则表达式
re.compile('[\s]*[|:][\s]*').split(text)
答案 2 :(得分:0)
您可以使用此简单的正则表达式'.[:|].'
,
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
请注意最后一个:)