这是问题Extract all substrings between two markers的继续。 @Daweo和@Tim Biegeleisen的answers适用于小字符串。
但是对于非常大的字符串,正则表达式似乎不起作用。这可能是因为字符串长度受到限制,如下所示:
>>> import re
>>> teststr = "&marker1\nThe String that I want /\n&marker1\nAnother string that I want /\n"
>>> for i in range(0, 23):
... teststr += teststr # creating a very long string here
...
>>> len(teststr)
603979776
>>> found = re.findall(r"\&marker1\n(.*?)/\n", newstr)
>>> len(found)
46
>>> found
['The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ', 'The String that I want ', 'Another string that I want ']
我该怎么做才能解决这个问题,并找出制造商start="&maker1"
和end="/\n"
之间的所有情况? re
可以处理的最大字符串长度是多少?
答案 0 :(得分:0)
我无法让re.findall
工作。现在,我确实使用re
来查找标记的位置并手动提取子字符串。
locs_start = [match.start() for match in re.finditer("\&marker1", mylongstring)]
locs_end = [match.start() for match in re.finditer("/\n", mylongstring)]
substrings = []
for i in range(0, len(locs_start)):
substrings.append(mylongstring[locs_start[i]:locs_end[i]+1])