繁体   English   中英

提取两个标记之间的所有子字符串以获得非常长的字符串

[英]Extract all substrings between two markers for a very long string

这是问题提取两个标记之间的所有子字符串的延续。 @Daweo 和 @Tim Biegeleisen 的答案适用于小字符串。

但是对于非常大的字符串,正则表达式似乎不起作用。 这可能是由于字符串长度的限制,如下所示:

>>> 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可以处理的最大字符串长度是多少?

我无法让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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM