Python 用于捕获多行文本正文的正则表达式

Python 用于捕获多行文本正文的正则表达式,python,regex,Python,Regex,因此,我有一些文本文档如下所示: 1a标题 字幕 描述 1b标题 副标题A 描述 副标题B 描述 2标题 副标题A 描述 副标题B 描述 副标题C 描述 我正试图用正则表达式捕捉由3个选项卡缩进的“描述”行。我遇到的问题是,有时描述行将换行到下一行,并再次被3个制表符缩进。以下是一个例子: 1演示 例子 这是我的描述文本体 试图用正则表达式捕获。 我想把这篇文章分成一组,最后是: 这是我试图用正则表达式捕获的描述文本体。 一旦我能够做到这一点,我还想“展平”的文件,使每个部分在一行字符分隔

因此,我有一些文本文档如下所示:

1a标题
字幕
描述
1b标题
副标题A
描述
副标题B
描述
2标题
副标题A
描述
副标题B
描述
副标题C
描述
我正试图用正则表达式捕捉由3个选项卡缩进的“描述”行。我遇到的问题是,有时描述行将换行到下一行,并再次被3个制表符缩进。以下是一个例子:

1演示
例子
这是我的描述文本体
试图用正则表达式捕获。
我想把这篇文章分成一组,最后是:

这是我试图用正则表达式捕获的描述文本体。
一旦我能够做到这一点,我还想“展平”的文件,使每个部分在一行字符分隔,而不是行和制表符。因此,我的示例代码将变成:

1->Demo->->->Example->->->->这是描述文本。。。
我将在Python中实现这一点,但任何正则表达式的指导都将不胜感激


向上
我已经更改了展平文本中的分隔符,以指示它以前的关系。ie;1个选项卡
->
,2个选项卡
->->
,3个选项卡
->->->->
,依此类推

此外,如果每个标题(章节)都有多个字幕(小节),则扁平文本的外观如下:

1a->Title->->Subtitle->->->Description
1b->Title->->->Subtitle A->->->->说明
1b->Title->->->副标题B->->->->说明
2->Title->->->Subtitle A->->->->Description
2->Title->->->Subtitle B->->->->说明
2->Title->->->副标题C->->->->说明

基本上只是为每个孩子“重用”父母(数字/标题)(副标题)。

这怎么样

re.findall(r'(?m)((?:^\t{3}.*?\n)+)', doc)
它还将捕获制表符和换行符,但这些可以稍后删除。

使用
re
python2:

text = "yourtexthere"
lines = re.findall("\t{3}.+", text)
不带制表符
“\t”

要获得最终输出,请执行以下操作:


您可以在不使用正则表达式的情况下执行此操作:

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']
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
其优点是,用3个凸耳分别缩进的不同部分保持可分离


好:下面是正则表达式中的完整解决方案:

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)
印刷品:

['This is the description text body that I am trying to capture with regex.', 
 'And Another Section On two lines']
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
限制:
您可以看到第二级和第三级是join,但我不知道您希望如何处理该格式

换行时,换行符的末尾是否有新的换行符,或者它只是这样打印?@Sniffer-是的,换行符是\n。是否使用制表符
\t
或空格?@Vik2015-它们是\t,而不是空格。这不是捕获两行。这是我得到的:
['\t\t\t这是我的描述文本体\n']
@tgxanaheimx它对我有效:)这是我测试的:@robertklep,它对我也不起作用。我得到了相同的输出:
re.findall(r'(?sm)((?:^\t{3}.*?\n)+',text.replace(“*4,”\t”)===>['\t\t这是我是的描述文本体]
你们都在使用Windows吗?我在Mac上。@robertklep,Linux,Fedora 18I喜欢这样,但它仍然在小组中捕捉线条
[“这是我正在使用正则表达式捕获的描述文本体”。]
@tgxanaheimx,
“\n”。加入(行)
@tgxanaheimx,抱歉!把你的问题看错了。现在我要尝试修正答案,我投票支持“干净”方法,没有使用正则表达式,它完全符合我的要求。文档“扁平化”部分呢?@tgxanaheimx所以你不是专门找正则表达式,你只是想要一个解析器?可以早一点说;)@robertklep-我正在寻找正则表达式,以防我需要转换到另一种编程语言,然而,drewk提供了一个简单有效的解析器,我正在考虑使用它,因为他确实提供了我所要求的,而不是正则表达式@德鲁克-是的,现在我们有进展了!问题:有没有办法像其他选项卡一样将第一个选项卡更改为
->
?所以
1a Title
应该是
1a->Title
。我还在研究处理规则,所以还有更多。谢谢是的,这是一个微不足道的变化。只需将
print'->'.join(sec)
更改为
print re.sub(r'(^\d+\w*)\s*',r'\1->','->'.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