[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

This regex wont work ... help requested!!

Posted on 2006-06-01
9
Medium Priority
?
236 Views
Last Modified: 2010-03-31
I am trying to get rid of empty lines in a .txt file.

This is an example file :

kk
zz

  fd
 
fdf


df

df



  dfd


Now if we can have a cool regex to take care of it.

Help will really be appreciated.

slick
0
Comment
Question by:slickdeals
7 Comments
 
LVL 14

Accepted Solution

by:
StillUnAware earned 500 total points
ID: 16810526
BufferedReader br = new BufferedReader(new FileReader("filename"));
String line = null;
while(((line = br.readLine()) != null) && line.length() > 0) {
    System.out.println(line);
}
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16810611
The example above uses some other technique while reading data from file. In case You have Your data already read into String, the this regex should help:

String contents = ... // Your file contents
contents = contents.replaceAll("\\n+", "\n"); // this replaces multiple new lines with only one
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16810653
just a correctness to StillUnAware's code

while((line = br.readLine()) != null) {
    if(line.length() > 0)
        System.out.println(line);
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:StillUnAware
ID: 16810713
wanted to make it smaller, did a logical mistake. Thanks for pointing it out :)
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 500 total points
ID: 16810949
as \r\n is also considered as line terminator in windows
this could be more general
replaceAll("(\\n|\\r\\n)+", "\n");
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 16811322
>>Now if we can have a cool regex to take care of it.

Not sure how cool this is but ...

if (line.matches("$\\s*))

delete it\ignore it
0
 

Assisted Solution

by:danielkullmann
danielkullmann earned 500 total points
ID: 16815409
content = content.replaceAll( "(\r?\n)(\r?\n)+", "\n" );
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question