如何用Notepad++正则表达式提取文本中To与Subject行(每组第1、4行)?
Extracting "To:" and "Subject:" Lines in Notepad++ (No More Bookmark Mismatches!)
Hey there! I’ve been in your shoes before—dealing with large text files and trying to yank specific lines can be frustrating when bookmarks don’t line up. Let’s fix this with two solid methods, starting with the most efficient one:
Method 1: Directly Remove Unwanted Lines (Fastest for 20k Rows)
This uses Notepad++’s find-and-replace with regex to wipe out everything except the lines you care about:
- Open your text file in Notepad++.
- Hit
Ctrl+Hto open the Replace dialog. - At the bottom, switch the Search Mode to Regular expression, and check the box for . matches newline (critical for handling multi-line content correctly).
- In the "Find what" field, paste this regex:
^(?!To: |Subject: ).*\R? - Leave the "Replace with" field empty.
- Click Replace All—boom, all lines that aren’t "To:" or "Subject:" will disappear, leaving exactly what you need.
Quick breakdown of the regex:
^anchors the match to the start of a line.(?!To: |Subject: )is a negative lookahead—it tells Notepad++ to skip lines that start with either "To: " or "Subject: "..*matches the rest of the line content.\R?matches any newline character (the?handles the last line in the file which might not have a newline).
Method 2: Fix Your Bookmark Workflow
If you still prefer using bookmarks, the issue was likely in how you were marking lines. Here’s the correct way:
- Open the Find dialog with
Ctrl+F. - Switch to Regular expression mode.
- In the "Find what" field, use this regex to match both line types at once:
^(To: |Subject: ).* - Click Mark All—this will bookmark every single line that starts with "To: " or "Subject: " (no mismatches this time!).
- Go to
Search > Bookmark > Copy Bookmarked Linesto copy them to your clipboard, then paste into your new editor.
Either method should work perfectly for your 20k-line file—Method 1 is faster since it avoids extra steps, but both are reliable.
内容的提问来源于stack exchange,提问作者ckp
相关产品推荐
相关产品推荐

