Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to split at >2 spaces or tabs

I'm trying to create a a regex that matches 2 or more spaces, or tabs, basically a combination of text.split(" ") and text.split("\t"). How do I create it?

My attempt: (but it doesn't work)

text.split(new RegExp("[  |\t]"))

Edit: This splits at spaces/tabs but I need to split at 2 spaces or more..

text.split("\\s+");
like image 728
Robin Rodricks Avatar asked Oct 15 '12 05:10

Robin Rodricks


2 Answers

\s{2,}

You can try in this way...! \s{2,} means 2 or more

I got this idea from this replace post Regex to replace multiple spaces with a single space

Demo: http://jsbin.com/akubed/1/edit

I agree with @Will comment - Add Tab space also

\s{2,}|\t
like image 74
Naga Harish M Avatar answered Oct 12 '22 23:10

Naga Harish M


String s="This      is      test";
    String [] as=s.split("\\t{2,}");
    for(int i=0;i<as.length;i++)
    System.out.println(as[i]);

This works for me.

like image 27
Shivam Bharadwaj Avatar answered Oct 12 '22 23:10

Shivam Bharadwaj