Thursday, October 8, 2009

Notepad++: Linebreaks in Regular Expressions

Believe it or not, Notepad++ does not support the use of linebreaks in regular expressions for its “find” feature—it’s regular expression support is quite weak actually.

But if you don’t use regular expressions often enough to look for an alternative to Notepad++, here’s a trick to get around the limitation.

Open the find dialog and select the “Replace” tab. Change the Search mode to “Extended”.  Now in the “Find what” field enter “\r\n” and in the “Replace with” field enter “__RN__” and click “Replace All”.image Now all the linebreaks in your file will be replaced __RN__ so you can use this string in your regular expession in place of the \r\n.  When you’re done, use the same method to convert __RN__ back to \r\n.

I used this method to identify multiple linebreaks in an .srt subtitles file and replace them with a single linebreak.

4 comments:

Nerys said...

The problem I am having is the opposite. I want to go the OTHER WAY. I have "unwanted" line breaks. ie at the ends of sentences.

I want to get rid of those but KEEP the breaks between paragraphs.

Suggestions?

I need to remove SINGLE instances of carriage returns but LEAVE double instances of carriage returns intact IE remove line breaks between sentences but LEAVE them between paragraphs.

Nerys said...

HAH off course after typing that I figured it out.

FIRST I replace \r\n\r\n IE doubles with a combo NOT likely to appear "DOUBLEBREAK"

then I replace \r\n with nothing getting rid of them

and then replace DOUBLEBREAK with \r\n

Worked a treat! now to figure out a way to AUTOMATE this time to see of notepadd++ has any sort of scripting capacity. :-)

Mike said...

Thanks for posting this. I feel pretty silly for not figuring it out myself, but this was a lifesaver.

I was trying to edit a csv and kill newlines that didn't end in commas:

Replace ([^,])\r\n
With \1

Anonymous said...

Thanks man, I really appreciate it

Post a Comment