我试图允许用户这样做:
让我们说最初的文字说:
"hello world hello earth"
当用户搜索“hello”时,它应该显示:
|hello| world |hello| earth
这就是我所拥有的:
m = re.compile(pattern)
i =0
match = False
while i < len(self.fcontent):
content = " ".join(self.fcontent[i])
i = i + 1;
for find in m.finditer(content):
print i,"\t"+content[:find.start()]+"|"+content[find.start():find.end()]+"|"+content[find.end():]
match = True
pr = raw_input( "(n)ext, (p)revious, (q)uit or (r)estart? ")
if (pr == 'q'):
break
elif (pr == 'p'):
i = i - 2
elif (pr == 'r'):
i = 0
if match is False:
print "No matches in the file!"
在哪里 :
pattern = user specified pattern
fcontent = contents of a file read in and stored as array of words and lines e.g:
[['line','1'],['line','2','here'],['line','3']]
但是它打印
|hello| world hello earth
hello world |hello| earth
如何合并两条线以显示为一条线?谢谢
编辑:
这是一个更大的搜索功能的一部分,其中模式..在这种情况下,单词“hello”是从用户传递的,所以我必须使用正则表达式搜索/匹配/查找器来查找模式。遗憾的是,替换和其他方法不起作用,因为用户可以选择搜索“[0-9]$”,这意味着将结束数字放在 | 之间