首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >regex + np++ +在页面顶部捕获字符串,从页面底部捕获字符串

regex + np++ +在页面顶部捕获字符串,从页面底部捕获字符串
EN

Stack Overflow用户
提问于 2018-05-08 01:43:10
回答 2查看 120关注 0票数 1

这是我的regex

我想要做的是能够捕获表和页码。示例输出或我想要的内容如下。我想要的表部分希望是显而易见的。页面编号是10 (10 4 Text Core statistics aggregated by the Statistics中的第一个数字),124 Text Core statistics aggregated by the Statistics 12中(最后一个数字)。

在np++中,我可以使用Table \d+获取所有表,但我也希望从同一页的瓶盖中获得页码。

我所拥有的:

代码语言:javascript
复制
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 

我想要的:

代码语言:javascript
复制
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)) --这可以在开始时找到行的开头和表。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-08 03:19:17

编辑实际上下载了notepad++并测试了正则表达式。

这将起作用:

代码语言:javascript
复制
(^Table \d+).*?(?=\d+\s(\d+\s)?Text Core)([^\n]+)(.*?(?=^Table \d+|\z))

它使用积极的前瞻性来搜索表号之后的第一个页码,然后抓取从那里到行尾的所有内容。然后,它抓住所有的东西,直到下一个“桌子”。请注意,您需要选中. matches newline框。

如果您想做一个替代品,请用\1\n\3\n替换它。regex101.com上的演示

票数 2
EN

Stack Overflow用户

发布于 2018-05-08 01:53:09

我至少可以提供一个部分的解决方案。按照以下模式进行替换:

代码语言:javascript
复制
^(?!Table)(?!\d+ (?:\d+ )?Text Core).*$

并用空字符串替换它。这应该删除以Table开头或包含Text Core的行之间的所有随机文本。下面是一个工作演示:

演示

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50224610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档