5

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+");
Robin Rodricks
  • 105,357
  • 137
  • 385
  • 594

2 Answers2

10
\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
Naga Harish M
  • 2,759
  • 2
  • 30
  • 45
  • 1
    I was using this but it didn't worked for one tab. Had do switch to `\s{2,}|\t`. I guess it is correct, since i had only a single tab. – Will Jan 12 '15 at 18:47
1
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.

Shivam Bharadwaj
  • 1,594
  • 20
  • 20