vendredi 17 avril 2015

RegEx to match lines consisting of whitespace only

I've gone through multiple expressions for hours but can't quite get one to match what I need exactly.


If I have the following input:



Hi



This


Is



A

Test


I am trimming it to:



Hi

This

Is

A

Test




All is good when the blank lines length are 0 (no String) however some inputs contains a few spaces (" ") within those blank lines and thus would like to check whether a string has 0:infinite number of whitespaces but no characters (simply a blank line).



ArrayList<Integer> listOfBlanks = new ArrayList<>();
for(int i = 0; i < arrayList.size(); i++) {
if(arrayList.get(i).isEmpty()) {
if(arrayList.get(i+1).isEmpty())
listOfBlanks.add(i+1);
}
}


String#isEmpty is only good when there are no whitespaces


Aucun commentaire:

Enregistrer un commentaire