简体   繁体   中英

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.

'There are many such sentences so I need to write a 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 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM