根据 Python 中的分隔符拆分和组合文本
splitting and combining text based on a delimiter in Python
我有一个包含字符串的列表列表。在各种正则表达式工作之后,我将我想用作分隔符的内容 @@@
插入到我的字符串中:
[['@@@this is part one and here is part two and here is part three and heres more and heres more'],
['this is part one@@@and here is part two and here is part three and heres more and heres more'],
['this is part one and here is part two@@@and here is part three and heres more and heres more']
['this is part one and here is part two and here is part three@@@and heres more and heres more']
['this is part one and here is part two and here is part three and heres more@@@and heres more']]
现在,我需要想出这个:
[['this is part one'],['and here is part two'],['and here is part three'], ['and heres more'], ['and heres more']]
到目前为止,我的尝试是臃肿的、老掉牙的,而且通常很难看。我发现自己分裂、组合和匹配。谁能就此类问题推荐一些一般性建议,以及使用什么工具来使其易于管理?
编辑请注意! and heres more
确实在理想输出中出现了两次!
我认为您实际上需要抓取 @@@
之后的所有字符,直到下一个 and
或字符串结尾。
>>> [[m] for x in l for m in re.findall(r'@@@(.*?)(?=\sand\b|$)', x[0])]
[['this is part one'], ['and here is part two'], ['and here is part three'], ['and heres more'], ['and heres more']]
我有一个包含字符串的列表列表。在各种正则表达式工作之后,我将我想用作分隔符的内容 @@@
插入到我的字符串中:
[['@@@this is part one and here is part two and here is part three and heres more and heres more'],
['this is part one@@@and here is part two and here is part three and heres more and heres more'],
['this is part one and here is part two@@@and here is part three and heres more and heres more']
['this is part one and here is part two and here is part three@@@and heres more and heres more']
['this is part one and here is part two and here is part three and heres more@@@and heres more']]
现在,我需要想出这个:
[['this is part one'],['and here is part two'],['and here is part three'], ['and heres more'], ['and heres more']]
到目前为止,我的尝试是臃肿的、老掉牙的,而且通常很难看。我发现自己分裂、组合和匹配。谁能就此类问题推荐一些一般性建议,以及使用什么工具来使其易于管理?
编辑请注意! and heres more
确实在理想输出中出现了两次!
我认为您实际上需要抓取 @@@
之后的所有字符,直到下一个 and
或字符串结尾。
>>> [[m] for x in l for m in re.findall(r'@@@(.*?)(?=\sand\b|$)', x[0])]
[['this is part one'], ['and here is part two'], ['and here is part three'], ['and heres more'], ['and heres more']]