在未知整数和子字符串上拆分字符串

时间:2018-08-06 11:45:25

标签: python split

我正在尝试将非常大的字符串切成碎片。但是,格式不是很一致。因此,我想对要分割的字符有一定的灵活性。

例如:

“这是一个字符串,我想在这里拆分 0 ,但我也希望它在这里 2拆分,以3个字符串结尾。”

所以我的定界符格式为int + 'here'

这样可能吗?

1 个答案:

答案 0 :(得分:4)

您可以将正则表达式用于此类问题,

import re
s = 'This is a string I would like to split 0 here but I also want it to split 2 here to end up with 3 strings.'
re.split(r'\d+\s*here', s)
# Output as : ['This is a string I would like to split ',
#' but I also want it to split ',
#' to end up with 3 strings.']