複数行の文字列を解析しようとしています。
次のように仮定します。
text = '''
Section1
stuff belonging to section1
stuff belonging to section1
stuff belonging to section1
Section2
stuff belonging to section2
stuff belonging to section2
stuff belonging to section2
'''
re モジュールの finditer メソッドを使用して、次のような辞書を取得したいと考えています。
{'section': 'Section1', 'section_data': 'stuff belonging to section1\nstuff belonging to section1\nstuff belonging to section1\n'}
{'section': 'Section2', 'section_data': 'stuff belonging to section2\nstuff belonging to section2\nstuff belonging to section2\n'}
私は次のことを試しました:
import re
re_sections=re.compile(r"(?P<section>Section\d)\s*(?P<section_data>.+)", re.DOTALL)
sections_it = re_sections.finditer(text)
for m in sections_it:
print m.groupdict()
しかし、これは次の結果になります。
{'section': 'Section1', 'section_data': 'stuff belonging to section1\nstuff belonging to section1\nstuff belonging to section1\nSection2\nstuff belonging to section2\nstuff belonging to section2\nstuff belonging to section2\n'}
したがって、section_data も Section2 と一致します。
また、最初のグループ以外のすべてに一致するように 2 番目のグループに指示しようとしました。しかし、これはまったく出力につながりません。
re_sections=re.compile(r"(?P<section>Section\d)\s+(?P<section_data>^(?P=section))", re.DOTALL)
次の re を使用できることはわかっていますが、2 番目のグループがどのように見えるかを説明する必要がないバージョンを探しています。
re_sections=re.compile(r"(?P<section>Section\d)\s+(?P<section_data>[a-z12\s]+)", re.DOTALL)
どうもありがとうございました!