如何在 Notepad++ 中查找/替换所有 CR/LF 字符?
我正在寻找与 Microsoft Word 中的 ^p 特殊字符等效的内容。
最佳答案
[\r\n]+
也应该有效
2012 年 3 月 26 日更新, 发布日期 Notepad++ 6.0 :
天啊,它现在确实可以工作了!!!
<小时/>
原始答案2008 (Notepad++ 4.x) - 2009-2010-2011 (Notepad++ 5.x)
实际上不,它似乎不适用于正则表达式...
但如果您有 Notepad++ 5.x,则可以使用“扩展”搜索模式并查找 \r\n
。这确实找到了您所有的 CRLF
。
(我意识到这与其他答案相同,但同样,“扩展模式”仅适用于 Notepad++ 4.9、5.x 及更多版本)
<小时/>自 2009 年 4 月以来,Notepad++ 网站上有一篇关于此主题的 wiki 文章:
“ How To Replace Line Ends, thus changing the line layout ”。
(由 georgiecasey 在他/她的 answer below 中提及)
一些相关摘录包括以下搜索过程:
<小时/>Simple search (Ctrl+F), Search Mode =
Normal
You can select an
EOL
in the editing window.
- Just move the cursor to the end of the line, and type Shift+Right Arrow.
- or, to select
EOL
with the mouse, start just at the line end and drag to the start of the next line; dragging to the right of theEOL
won't work. You can manually copy theEOL
and paste it into the field for Unix files (LF
-only).Simple search (Ctrl+F), Search Mode = Extended
The "Extended" option shows
\n
and\r
as characters that could be matched.
As with the Normal search mode, Notepad++ is looking for the exact character.
Searching for\r
in a UNIX-format file will not find anything, but searching for\n
will. Similarly, a Macintosh-format file will contain\r
but not\n
.Simple search (Ctrl+F), Search Mode = Regular expression
Regular expressions use the characters
^
and$
to anchor the match string to the beginning or end of the line. For instance, searching forreturn;$
will find occurrences of "return;" that occur with no subsequent text on that same line. The anchor characters work identically in all file formats.
The '.' dot metacharacter does not match line endings.[Tested in Notepad++ 5.8.5]: a regular expression search with an explicit
\r
or\n
does not work (contrary to the Scintilla documentation).
Neither does a search on an explicit (pasted) LF, or on the (invisible) EOL characters placed in the field when an EOL is selected. Advanced search (Ctrl+R) without regexpCtrl+M will insert something that matches newlines. They will be replaced by the replace string.
I recommend this method as the most reliable, unless you really need to use regex.
As an example, to remove every second newline in a double spaced file, enter Ctrl+M twice in the search string box, and once in the replace string box.Advanced search (Ctrl+R) with Regexp.
Neither Ctrl+M,
$
nor\r\n
are matched.
同一个维基百科还提到了十六进制编辑器替代方案:
- Type the new string at the beginning of the document.
- Then select to view the document in Hex mode.
- Select one of the new lines and hit Ctrl+H.
- While you have the Replace dialog box up, select on the background the new replacement string and Ctrl+C copy it to paste it in the Replace with text input.
- Then Replace or Replace All as you wish.
Note: the character selected for new line usually appears as
0a
.
It may have a different value if the file is in Windows Format. In that case you can always go toEdit -> EOL Conversion -> Convert to Unix Format
, and after the replacement switch it back andEdit -> EOL Conversion -> Convert to Windows Format
.
关于regex - 在Notepad++中查找CRLF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/133965/