我有一个巨大的文本文件,其中包含来自A-Z的随机字母,我想提取一些字符。棘手的部分是给出以下输入:
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\";");
和模式$("#" + this.divId).append('<object id="' + this.pdfObjectId + '" data="' + yourserver + path + file+'" type="application/pdf" width="768" height="1024"></object>');
,我想提取每个匹配项,直到字符串末尾。我想要以下输出:
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("pragma", "no-cache");
response.setContentType("application/pdf");
答案 0 :(得分:0)
您可以使用str.find()
查找出现模式的索引。然后,您可以相应地对字符串进行切片。一个实现可能看起来像这样:
def find(inp, what):
matches = []
while what in inp:
idx = inp.find(what)
matches.append(inp[idx:])
# remove the previous pattern from the string
inp = inp[idx+len(what):]
return matches
您可以将其与find("AFVAJFLDVAJPQDVAJDSNJKVAJGHD", "VAJ")
一起使用。
答案 1 :(得分:0)
这要求具有子组匹配的正则表达式。 (https://docs.python.org/3.5/library/re.html#match-objects)
我的测试文件data.txt
:
QWEEEFVAJFLDVAJPQDVAJDSNJKVAJGHD
AFVAJFLDVAJPQDVAJDSNJKHFGHERQWFS
ONLY_TWO_VAJsOOVAJ123VAQQWERTY
START_VAJs_with_more_VAJ123VAJ_space_between
AAPVAJRCGVAJJKYVAJJJJJJJJVAJOOOO
AAPVAJRCGVAJJKYVAJJJJJJJJQQQOOOOO
Python代码:
import re
pattern = "VAJ"
re_str = pattern + "..." + "(" + pattern + "..." +"(" + pattern + "(.*)))"
regex = re.compile(re_str)
regex_extra = re.compile(pattern + ".*")
for line in open("data.txt"):
line = line.strip()
match = regex.search(line)
if match:
result = list()
result.append(match.group(0)) # entire regex match
result.append(match.group(1)) # outer regex parenthesis'ed group
result.append(match.group(2)) # middle regex parenthesis'ed group
# Most inner regex parenthesis'ed group contains rest of the line.
# Use this to find extra pattern.
#
the_rest = match.group(3)
match_extra = regex_extra.search(the_rest)
if match_extra: # If one more <pattern> in the rest of the line
result.append(match_extra.group(0)) # add it to the result list
# Output
print(result)
输出:
['VAJFLDVAJPQDVAJDSNJKVAJGHD', 'VAJPQDVAJDSNJKVAJGHD', 'VAJDSNJKVAJGHD', 'VAJGHD']
['VAJFLDVAJPQDVAJDSNJKHFGHERQWFS', 'VAJPQDVAJDSNJKHFGHERQWFS', 'VAJDSNJKHFGHERQWFS']
['VAJRCGVAJJKYVAJJJJJJJJVAJOOOO', 'VAJJKYVAJJJJJJJJVAJOOOO', 'VAJJJJJJJJVAJOOOO', 'VAJOOOO']
['VAJRCGVAJJKYVAJJJJJJJJQQQOOOOO', 'VAJJKYVAJJJJJJJJQQQOOOOO', 'VAJJJJJJJJQQQOOOOO']
此代码的文件大小不是问题,只要最长的行在内存中适合几次,就可以了。