46

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?

theSpyCry
  • 12,073
  • 28
  • 96
  • 152
  • Make sure to switch `matches newline` option on as it was mentioned in [this thread](http://stackoverflow.com/questions/13934430/notepad-multiline-regex) – aljipa Jan 16 '14 at 21:23
  • 2
    @aljipa that option is not necessary, when newlines are expected in a specific place. – ANeves Feb 16 '14 at 17:59

10 Answers10

59

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

Robb
  • 3,801
  • 1
  • 34
  • 37
  • 4
    oh.. well I've tried a\nb\nc .. forgot the carriage return. Thanks ! – theSpyCry Dec 09 '10 at 13:32
  • 1
    sometimes is needed searching newlines with regex (if you want to make more complex search including newlines). it is not possible as detailed in next answer – NeDark May 04 '14 at 09:44
  • 1
    Note that I tried in vain to get newline characters working Notepad++ 5.9.3 using "Regular Expression" (rather than "Extended")! It works fine in Notepad++ 6.0 though - see darioo's answer below. – SharpC Mar 17 '15 at 16:30
  • 1
    I don't think this is correct, question asked for regex, not extended mode. it is possible in regex \x0D is newline and \x0A is carriage return see answer http://stackoverflow.com/a/4398728/4459336 – ClearBlueSky85 Dec 29 '16 at 17:54
35

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.

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
darioo
  • 46,442
  • 10
  • 75
  • 103
  • 1
    I think this answer is wrong. See Robb's answer and mine to verify that this answer is wrong. – TechTravelThink Dec 09 '10 at 13:33
  • @StartClass0830: I've tried yours and Robb's answers and I can't get multiline matching to work. Edit: yep, it does work when using extended search. But not when using regular expressions. You were right. – darioo Dec 09 '10 at 13:38
  • 1
    The assumption is the line ends with \r \n. Perhaps in your environment, the file has lines which end with \n only (without the \r). – TechTravelThink Dec 09 '10 at 14:47
  • Anyways.. is there anything better than Notepad++ with full REGEX support ? – theSpyCry Dec 09 '10 at 15:32
  • 1
    @PaN1C_Showt1Me: Well, there's always Vim and Emacs. I've used both and they really powerful regex support. But the learning curve always puts people off. I'd suggest you visit http://www.vim.org/, download Vim and go through the tutorial before trying to do anything serious with it. – darioo Dec 09 '10 at 21:51
  • This saved me time. I was trying to mix & match regex with newline. – Deep Jul 13 '11 at 10:51
14
^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.

TechTravelThink
  • 3,014
  • 3
  • 20
  • 13
5

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.

andronikus
  • 4,125
  • 2
  • 29
  • 46
Kuchara
  • 622
  • 7
  • 16
  • This works fine on a small document. Tried it on a very large file (>1M lines). Notepad++ completely hanged (may be due to too less memory). The problem, with this method is: your whole document ends up on one line when replacing all new line characters. – Dave May 04 '12 at 07:33
  • A potential modification to deal with a large document would be to close the document, and use Kuchara's pragmatic workaround using Replace In Files with a Directory and file name Filter that will only modify the intended file. Thus, no need to open the file in the Scintilla editor itself while the transformation is being done on the single line. – JMD Sep 28 '12 at 21:58
1

For Notepad 6 and beyond, do this as a regular expression:

  • Select Search Mode > Regular expression (w/o . matches newline)
  • And in the Find what Textbox : a[\r\n]b[\r\n]+c[\r\n]

or if you are looking at the (Windows or Unix) file to see its line breaks as \r\n or \n then you may find it easier to use Extended Mode:

  • Select Search Mode > Extended (\n, \r, \t, \0, \x...)
  • And in the Find what Textbox for Windows: a\r\nb\r\nc\r\n
  • Or in the Find what Textbox for Unix: a\nb\nc\n

Wasn't clear if the OP intent is to select the trailing line return (after the 'c') as well, as would be necessary to remove the lines.

To not select the trailing line return, as appropriate for replacing with a non-empty string, simply remove the final line return from the matching statement.

Note that if there should be a match on the last line of the string, without a matching trailing line return, the match fails.

Vic Colborn
  • 1,895
  • 1
  • 17
  • 21
0

Select Search Mode Which is Extended (\n, \r, \t, \0, \x...)

\n is new line and such This is Manuel

0

Find: "(^a.$)\r\n(b.)\r\n^(c.*)$" - pickup 3 whole lines, only storing data

Replace with: "\1\2\3" - Put down (replay) data

Works fine in Regex with Notepad++ v7.9.5

Place holders: ^ Start and $ End of line can be inside or out of ()store as shown, though clearly not necessary in given example. Note "[^x]" is different - here "^" is "NOT".

Advantage of storing and replay allows much more complicated pattern match without having to type in again what you want to end up with, and even change of replay: "\2\3\1" for "bca"

Phil
  • 11
  • 1
0

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. :(

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
Markstar
  • 639
  • 9
  • 24
0

I have run accross this little issue when the document is windows CR/LF

If you click the box for . to match newlines you need .. to match CR/LF so if you have

<blah><blah>",
"<more><blah>

you need to use ",.." to match some string comma cr/lf another string

lkreinitz
  • 281
  • 3
  • 9
-4

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.

Jack
  • 1