在Notepad++中用正则表达式捕获表格及对应页码的技术问询
Linking Table Captures to Their Corresponding Page Numbers in Notepad++
Got it, let's solve this problem of pairing your table entries with the correct page numbers from the bottom of each page. Since you're already using Table \d+ to catch all tables, we just need to extend that regex to reach the page footer and grab the relevant page number (either the first or last number in the footer text, as you described).
The Regex You Need (PCRE-Compatible for Notepad++)
(Table \d+: .*?)(?s)(?:(?!Table \d+:).)*?(?:^|\s)(\d+)\s.*?(?:\s(\d+)(?:$|\s))?
Breakdown of the Pattern
(Table \d+: .*?): This captures the table header line (fromTable X:up to the end of that line, using non-greedy matching to stop at the first line break).(?s): Enables "single-line mode" so the.character matches newlines—critical for spanning from the table down to the page footer.(?:(?!Table \d+:).)*?: This non-capturing group skips all content between the table and the next table (or end of document), ensuring we only look at the current page's content.(?:^|\s)(\d+)\s.*?: Catches the first number in the footer (matches a number that starts a line or follows a space, then skips the rest of the footer text).(?:\s(\d+)(?:$|\s))?: An optional group to catch the last number in the footer (matches a number that precedes a line end or space). This covers both your example footer formats.
How to Use It in Notepad++
- Hit
Ctrl+Fto open the Find dialog. - Switch to the Regular expression mode, and make sure the
Match newlinecheckbox is checked (this activates the single-line mode we need). - Paste the regex above, then click "Find All" to see all matches. If you want to format the results (e.g., list tables with their page numbers), use the "Replace" tab:
- For example, replace with
$1 - Page Number: $2$3to get output likeTable 1: bifrost &... - Page Number: 10orTable 1: bifrost &... - Page Number: 12.
- For example, replace with
Example Matches
- For footer text
10 4 Text Core statistics aggregated by the Statistics, the regex captures the table and page number10. - For footer text
4 Text Core statistics aggregated by the Statistics 12, it captures the table and page number12. - If a footer has both start and end numbers (like
10 ... 12), it'll grab both—you can adjust the replacement string to use only$2or$3depending on which number you need.
Quick Notes
- Make sure your document's footers are consistently placed after the table content of each page, before the next table starts.
- If your document uses page breaks (like
\f), you can tweak the regex to respect those by changing the middle group to(?:(?!Table \d+:|\f).)*?\f?—this ensures we don't cross page boundaries when matching.
内容的提问来源于stack exchange,提问作者HattrickNZ
相关产品推荐
相关产品推荐

