如何删除Notepad++以外的所有内容?提取userId附近的ID
Hey there! Let's tackle your two Notepad++ tasks step by step—super straightforward once you know the regex tricks.
The easiest way to strip out unwanted content is using regular expression replace. Here's how:
- Hit
Ctrl+Hto open the Replace window. - Switch to the "Regular expression" mode (make sure "Wrap around" is checked; tick "Match newline" if you need to target content across lines).
- Example scenario: If you only want to keep lines that mention "userId", use this setup:
- Find what:
^(?!.*userId).*$\r?\n? - Replace with: (leave this blank)
- Click "Replace All"—boom, all lines without "userId" are instantly removed.
- Find what:
- If you need to preserve a specific format (like all
id: XXXentries), adjust the regex to target your desired content, or use the "Find All in Current Document" feature: search for your target pattern, right-click the matches, select "Copy All", then paste into a new clean document.
Since you didn't share the exact required output style, I'll give you a flexible regex method you can tweak to fit your needs:
First, identify how your userId is formatted (e.g., "userId": "12345", userId=abc789, etc.). Let's walk through two common cases:
Case 1: JSON-style "userId": "12345"
- Open Replace (
Ctrl+H) → Regular expression mode. - Find what:
.*"userId": "(\d+)".* - Replace with:
$1(or your desired format, likeUser ID: $1if you need labels for each entry) - Click "Replace All", then go to
Edit > Line Operations > Remove Empty Linesto clean up leftover blank rows.
Case 2: Plain text userId=abc789 (alphanumeric IDs)
- Find what:
.*userId=(\w+).* - Replace with:
$1(or your custom style) - Run the same empty line cleanup step as above to tidy the result.
If you need to keep a bit of context around the userId (like the line before/after), use a multi-line regex. For example, to keep the userId line plus the line immediately after:
- Find what:
.*userId.*\r?\n.*\r?\n? - Replace with:
$0(this retains the matched content) - Then remove any remaining empty lines to polish the final output.
内容的提问来源于stack exchange,提问作者Mostafa A alsharqawy

