テキストを区切るために findall を使用しています。
この式 re.findall(r'(. ?)(\$. ?\$)' から始めましたが、最後のテキストが見つかった後のデータが得られません。'6\n\n '
最後のテキストを取得するにはどうすればよいですか?
ここに私のpythonコードがあります:
#!/usr/bin/env python
import re
allData = '''
1
2
3 here Some text in here
$file1.txt$
4 Some text in here and more $file2.txt$
5 Some text $file3.txt$ here
$file3.txt$
6
'''
for record in re.findall(r'(.*?)(\$.*?\$)|(.*?$)',allData,flags=re.DOTALL) :
print repr(record)
これに対して得られる出力は次のとおりです。
('\n1\n2\n3 here Some text in here \n', '$file1.txt$', '')
('\n4 Some text in here and more ', '$file2.txt$', '')
('\n5 Some text ', '$file3.txt$', '')
(' here \n', '$file3.txt$', '')
('', '', '\n6\n')
('', '', '')
('', '', '')
私は本当にこの出力が欲しいです:
('\n1\n2\n3 here Some text in here \n', '$file1.txt$')
('\n4 Some text in here and more ', '$file2.txt$')
('\n5 Some text ', '$file3.txt$')
(' here \n', '$file3.txt$')
('\n6\n', '', )
全体像を表示する必要がある場合の背景情報。
あなたが興味を持っている場合、私はこれをpythonで書き直しています。残りのコードは管理下にあります。私はfindallからあまりにも多くのものを得ています。