python - 如何使用正则表达式循环过滤特定字符之前和之后的部分句子
问题描述
'我想提取“:”和“|”前后的文本 使用正则表达式并将其分成扬声器和标题。
'这样的句子很多,所以我需要写一个循环'
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)
推荐阅读
- javascript - 在函数中插入变量
- php - 如何使用 JavaScript 从服务器获取数据?(第一次将后端添加到 React 应用程序)
- excel - VBA根据单元格的定义名称设置单元格值
- javascript - 如何有效地将大文件加载到 IndexedDB 存储中?我的应用程序在超过 100,000 行时崩溃
- c# - MVVM wpf:如何连接我之前制作的 ObservableCollection 遇到 ViewModel
- algorithm - 我们真的需要在约翰逊算法中添加一个额外的节点吗?
- c++ - 这是一种什么样的说法?(第6章,使用C++的原理与实践)
- java - 在java中使用String builder反转数字
- solr - 无需搜索即可从 Solr 读取所有文档(仅在可能的情况下使用文档 ID)
- java - JavaFX MediaPlayer 没有显示我所有音频文件的专辑封面,即使它们有一个