2

したがって、次のようなテキストドキュメントがいくつかあります。

1a  Title
        Subtitle
            Description
1b  Title
        Subtitle A
            Description
        Subtitle B
            Description
2   Title
        Subtitle A
            Description
        Subtitle B
            Description
        Subtitle C
            Description

正規表現を使用して、3 つのタブでインデントされた「説明」行をキャプチャしようとしています。私が抱えている問題は、説明行が次の行に折り返され、再び 3 つのタブでインデントされることがあります。次に例を示します。

1   Demo
        Example
            This is the description text body that I am
            trying to capture with regex.

このテキストを 1 つのグループにまとめて、次のようにしたいと考えています。

This is the description text body that I am trying to capture with regex.

これができるようになったら、ドキュメントを「フラット化」して、行やタブではなく文字で区切られた 1 行の各セクションを作成したいと考えています。したがって、私のコード例は次のようになります。

1->Demo->->Example->->->This is the description text...

これを Python で実装しますが、正規表現のガイダンスをいただければ幸いです。


UPTADE
フラット化されたテキストの区切り文字を変更して、以前の関係であることを示しました。すなわち; 1 タブ->、2 タブ->->、3 タブ->->->など。

さらに、タイトル (セクション) ごとに複数のサブタイトル (サブセクション) がある場合、平坦化されたテキストは次のようになります。

1a->タイトル->->字幕->->->説明
1b->タイトル->->字幕A->->->説明
1b->タイトル->->字幕B->->->説明
2->タイトル->->字幕A->->->説明
2->タイトル->->字幕B->->->説明
2->タイトル->->字幕C->->->説明

基本的には、各子 (サブタイトル) の親 (番号/タイトル) を「再利用」するだけです。

4

3 に答える 3

2

正規表現なしでこれを行うことができます:

txt='''\
1\tDemo
\t\tExample
\t\t\tThis is the description text body that I am
\t\t\ttrying to capture with regex.
\t\tSep
\t\t\tAnd Another Section
\t\t\tOn two lines
'''

cap=[]
buf=[]
for line in txt.splitlines():
    if line.startswith('\t\t\t'):
        buf.append(line.strip())
        continue
    if buf:    
        cap.append(' '.join(buf))
        buf=[]
else:
    if buf:    
        cap.append(' '.join(buf))      

print cap

版画:

['This is the description text body that I am trying to capture with regex.', 
 'And Another Section On two lines']

利点は、3 つのタブで別々にインデントされた個別のセクションが分離可能なままであることです。


OK: 正規表現での完全な解決策は次のとおりです。

txt='''\
1\tDemo
\t\tExample
\t\t\tThis is the description text body that I am
\t\t\ttrying to capture with regex.
2\tSecond Demo
\t\tAnother Section
\t\t\tAnd Another 3rd level Section
\t\t\tOn two lines
3\tNo section below
4\tOnly one level below
\t\tThis is that one level
'''

import re

result=[]
for ms in re.finditer(r'^(\d+.*?)(?=^\d|\Z)',txt,re.S | re.M):
    section=ms.group(1)
    tm=map(len,re.findall(r'(^\t+)', section, re.S | re.M))
    subsections=max(tm) if tm else 0
    sec=[re.search(r'(^\d+.*)', section).group(1)]
    if subsections:
        for i in range(2,subsections+1):
            lt=r'^{}([^\t]+)$'.format(r'\t'*i)
            level=re.findall(lt, section, re.M)
            sec.append(' '.join(s.strip() for s in level))

    print '->'.join(sec)

版画:

1   Demo->Example->This is the description text body that I am trying to capture with regex.
2   Second Demo->Another Section->And Another 3rd level Section On two lines
3   No section below
4   Only one level below->This is that one level

制限:

1) This is limited to the format you described.
2) It will not handle reverse levels properly:
    1 Section 
         Second Level
             Third Level
         Second Level Again       <== This would be jammed in with 'second level'
    How would you handel multi levels?

3) Won't handle multiline section headers:

    3    Like
         This

完全な例でこれを実行します。

1a  Title->Subtitle->Description Second Line of Description
1b  Title->Subtitle A Subtitle B->Description Description
2   Title->Subtitle A Subtitle B Subtitle C->Description Description Description

2 番目と 3 番目のレベルが結合されていることがわかりますが、その書式設定をどのように処理するかはわかりません。

于 2013-10-17T14:30:38.627 に答える
0

repython2 の使用:

text = "yourtexthere"
lines = re.findall("\t{3}.+", text)

タブなし"\t":

text = "yourtexthere"
lines = [i[3:] for i in re.findall("\t{3}.+", text)]

最終出力を取得するには:

...<br>
"\n".join(lines)


修理:

まだあまり良くありませんが、私はそれに取り組んでいます:

import re
text = "..."
out = [i for i in re.findall("\t{2,3}.+", text.replace("    ", "\t"))]
fixed = []
sub = []
for i in out:
    if not i.startswith("\t"*3):
        if sub: fixed.append(tuple(sub)); sub = []
    else:
        sub.append(i)
if sub:
    fixed.append(tuple(sub))
print fixed
于 2013-10-17T14:10:01.817 に答える