Notepad++ newline in regex

ε祈祈猫儿з 提交于 2019-12-04 16:08:53

问题


Suppose you have this file:

x
a
b
c
x
x
a
b
c
x
x

and you want to find the sequence abc (and select the whole 3 lines) with Notepad++ . How to express the newline in regex, please?


回答1:


Notepad++ can do that comfortably, you don't even need regexes

In the find dialogue box look in the bottom left and switch your search mode to Extended which allows \n etc.

As odds on you're working on a file in windows format you'll be looking for \r\n (carriage return, newline)

a\r\nb\r\nc

Will find the pattern over three lines




回答2:


Update 18th June 2012

With the new Notepad++ v6, you can indeed search for newlines with regexes. So you can just use

a\r\nb\r\nc

even with regular expressions to accomplish what you want. Note \r\n is Windows encoding of line-breaks. In Unix files, its just \n.

Unfortunately, you can't do that in Notepad++ when using regex search. Notepad++ is based on the Scintilla editor component, which doesn't handle newlines in regex.

You can use extended search for newline searching, but I don't think that will help you search for 3 lines.

More info here.

Update: Robb and StartClass0830 were right about extended search. It does work, but not when using regular expressions search.




回答3:


^a\x0D\x0Ab\x0D\x0Ac

This will work \x0D is newline and \x0A is carriage return. Assumption is that each line in your file ends with ascii 10 and 13.




回答4:


I found a workaround for this. Simply, in Extended mode replace all \r\n to a string that didn't exist in the rest of the document eg. ,,,newline,,, (watch out for special regexp chars like $, &, and *). Then switch to Regexp mode, do some replacements (now newline is ,,,newline,,,). Next, switch to Extended mode again and replace all ,,,newline,,, to \r\n.




回答5:


a\r\nb\r\nc works for me, but not ^a\x0D\x0Ab\x0D\x0Ac

Hmm, too bad that newline is not working with regular expressions. Now I have to go back to Textpad again. :(




回答6:


In Notepad++ you can also try highlighting the desired part of the text and then pressing CTRL+J.

That would justify the text and thus removing all line endings.



来源:https://stackoverflow.com/questions/4398613/notepad-newline-in-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!