这是我的regex
我想要做的是能够捕获表和页码。示例输出或我想要的内容如下。我想要的表部分希望是显而易见的。页面编号是10 (10 4 Text Core statistics aggregated by the Statistics中的第一个数字),12在4 Text Core statistics aggregated by the Statistics 12中(最后一个数字)。
在np++中,我可以使用Table \d+获取所有表,但我也希望从同一页的瓶盖中获得页码。
我所拥有的:
Table 1: bifrost
<lots of randon text >
10 4 Text Core statistics aggregated by the Statistics
<lots of randon text >
4 Text Core statistics aggregated by the Statistics 11
Table 2: homestead
<lots of randon text >
4 Text Core statistics aggregated by the Statistics 12
<lots of randon text >
12 4 Text Core statistics aggregated by the Statistics
Table 3: homestead
<lots of randon text >
12 4 Text Core statistics aggregated by the Statistics 我想要的:
Table 1: bifrost
10 4 Text Core statistics aggregated by the Statistics
Table 2: homestead
4 Text Core statistics aggregated by the Statistics 12
Table 3: homestead
12 4 Text Core statistics aggregated by the Statistics EDIT1
关于以下可能的答案,如果这有助于:
(Table \d*).*?(?=\d+\s(\d+\s)?Text Core)([^\n]+)(.*?(?=^Table \d+|\z)) --什么都找不到
(Table \d*).* - works找到Table行
(Table \d*) --工作程序查找行的Table和数字部分(例如,Table 1)
.*?(?=\d+\s(\d+\s)?Text Core) - works在以数字开头的行的开头查找数字(^0长度匹配)
(?=\d+\s(\d+\s)?Text Core) - works在以数字开头的行的开头查找数字(^0长度匹配)
([^\n]+) - works查找带有文本的行(也就是说,它突出显示所有文本)
(.*?(?=^Table \d+|\z)) --这可以在开始时找到行的开头和表。
发布于 2018-05-08 03:19:17
编辑实际上下载了notepad++并测试了正则表达式。
这将起作用:
(^Table \d+).*?(?=\d+\s(\d+\s)?Text Core)([^\n]+)(.*?(?=^Table \d+|\z))它使用积极的前瞻性来搜索表号之后的第一个页码,然后抓取从那里到行尾的所有内容。然后,它抓住所有的东西,直到下一个“桌子”。请注意,您需要选中. matches newline框。
如果您想做一个替代品,请用\1\n\3\n替换它。regex101.com上的演示
发布于 2018-05-08 01:53:09
我至少可以提供一个部分的解决方案。按照以下模式进行替换:
^(?!Table)(?!\d+ (?:\d+ )?Text Core).*$并用空字符串替换它。这应该删除以Table开头或包含Text Core的行之间的所有随机文本。下面是一个工作演示:
https://stackoverflow.com/questions/50224610
复制相似问题