Tastybrownies
Tastybrownies

Reputation: 937

Replacing series of new lines in File

I've ran into a bit of a rough spot in this Java program I'm writing an thought I would ask for some help. I'm using regex to replace certain lines in a file being read in and not getting the desired result. I want to replace all series of 3 new lines in my file and thought this would be straight forward since my regex is working in notepad++ but I guess not. Below is what an example of what the file is like:

FIRST SENTENCECRLF
CRLF
CRLF
CRLF
CRLF
CRLF
SECOND SENTENCECRLF

So, in other words, I am wanting to remove 3 of those carriage return\line feed instances between the first and second sentence lines. Below is what I've tried so far. The first tried in Java results in no change to the file (works in Notepad++ fine). The second, pretty much the same as the first works in notepad++ but not Java. The third is pretty much the exact same case as the other two. Anyone have any helpful suggestions as to what might work in this situation. At this point anything would be greatly appreciated!

^(\r\n){3}

^\r\n(\r\n)(\r\n)

^\r\n\r\n\r\n

Upvotes: 0

Views: 99

Answers (1)

Iceberg
Iceberg

Reputation: 396

Try the following regex:

(?m)^(\r\n){3}

The (?m) enables multi-line mode in Java, as explained in How to use java regex to match a line

Upvotes: 1

Related Questions