python - Python - 在多个子字符串之间捕获多个子字符串
问题描述
我拥有的数据格式非常糟糕 .txt 。我试图在这些开始和结束字符串之间捕捉意义完整的单词/句子。现在,我在一个文本中发现了大约 4 种类型的子字符串模式。我正在尝试捕获这些多个开始和结束子字符串之间的字符串。我能够正确捕获第一个字符串出现,但不能正确捕获第二个、第三个……等。
开始和结束字符串:FOO、BARS、BAR、BAR2
text = 'I do not want this FOO string1 BARS I do not want this FOO string 2 BAR I do not want this FOO string3 BAR2 I do not want this FOO string4 BARS '
snippet1 = text[text.index('FOO')+len('FOO'):text.index('BARS')] \
if text[text.index('FOO')+len('FOO'):text.index('BARS')] else ''
snippet2 = text[text.index('FOO')+len('FOO'):text.index('BAR')] \
if text[text.index('FOO')+len('FOO'):text.index('BAR')] else ''
snippet3 = text[text.index('FOO')+len('FOO'):text.index('BAR2')] \
if text[text.index('FOO')+len('FOO'):text.index('BAR2')] else ''
# print(type(snippet1))
print('')
print('snippet1:',snippet1) #Output: snippet1: string1
print('')
print('snippet2',snippet2) # Output: snippet2 string1
print('')
print('snippet3',snippet3) # Output: snippet3 string1 BARS I do not want this FOO string2 BAR I do not want this FOO string3
# How do I get this output? Is it possible to code this?
snippet1: string1
snippet2: string2
snippet3: string3
解决方案
IIUC:您可以使用以下方法执行此操作regex
:
import re
txt='I do not want this FOO string1 BARS I do not want this FOO string 2 BAR I do not want this FOO string3 BAR2 I do not want this FOO string4 BARS '
re.findall('FOO(.*?)BAR', txt)
将生成匹配字符串列表,如下所示:
[' string1 ', ' string 2 ', ' string3 ', ' string4 ']
更新匹配多个关键字:
import re
txt='I do not want this FOO string1 BARS I do not want this FOO string 2 SECTION I do not want this FOO string3 BAR2 I do not want this FOO string4 BARS'
re.findall('FOO(.*?)[BAR|SECTION]', txt)
将导致:
[' string1 ', ' string 2 ', ' string3 ', ' string4 ']
推荐阅读
- ios - 如何将纯色图像插入照片库集合视图
- java - 在 java 中使用 PATCH API(之前的问题已被删除)
- python - Or-Tools CpSolver 获取最小和最大总和或绝对值
- javascript - 每当我单击分页编号 4 或 8 或 12 时,分页布局就会消失
- javascript - 如何使用 javascript 和 HTML 将变量设置为 ID 和类中的内容
- oauth-2.0 - 最终用户登录和后端资源访问都可以使用单个应用程序注册吗?(作为公共和机密应用程序)
- java - 如何在 Spring 中处理“org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误”?
- php - 如何限制某些名称不能在 html 名称输入中使用或写入?
- python - 如何在我的 django 视图中删除 keyerror?
- javascript - filter an array with same elements