ユーザーにこれを許可しようとしています:
最初に、テキストに次のように書かれているとしましょう:
"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
2 つの行を結合して 1 つに表示するにはどうすればよいですか? ありがとう
編集:
これは、より大きな検索関数の一部であり、パターン..この場合、単語「hello」がユーザーから渡されるため、正規表現の検索/一致/検索を使用してパターンを見つける必要があります。ユーザーは "[0-9]$" を検索することを選択でき、それは終了番号を | の間に挿入することを意味するため、置換およびその他の方法は残念ながら機能しません。