Take the 2-minute tour ×

Let's say I have a String, foo, with values separated by whitespace:

  • [value 1] [value 2] [value 3] ... [value n]

What would the regular expression be to split(...) foo, such that the resulting String array contained all values except value 1? Is this even possible?

Here's what I have thus far:

  • String[] array = foo.split("\\s");

And that's not much, I know.


I'm looking to accomplish this purely through regular expressions. If this is not possible, please let me know!

share|improve this question
You can split the string into pieces based on your regular expression. –  Brian Bauman Aug 11 '11 at 15:03
@DeathMagus, Right, but I was hoping there was an elegant way to exclude the first value using regex. –  mre Aug 11 '11 at 15:04
You can always ignore the first match. –  dlev Aug 11 '11 at 15:04
@dlev, See my comment above. –  mre Aug 11 '11 at 15:05
Regular expressions are for pattern matching. I don't believe there's a method to perform a second tier of logic on your pattern from within the expression. –  Brian Bauman Aug 11 '11 at 15:06

4 Answers 4

up vote 3 down vote accepted

Once you've split your string into an array of values, loop through the array and do whatever you need, skipping the first iteration.

for(i=1; i<array.count(); i++){
    //Act on the data value
share|improve this answer
No offense, but this is painfully obvious. I don't want to implement a solution like this. I want to be able to do this purely through regex, if that's possible. –  mre Aug 11 '11 at 15:06
@mre: Then that should have been stated in the original question. –  Evan Mulawski Aug 11 '11 at 15:07
@Evan Mulawski, It was. Try reading the question again, don't just skim over it. –  mre Aug 11 '11 at 15:08
@mre: You also asked if it was possible. This post is assuming it isn't and offering a viable alternative. You are using Split to get an array to iterate over, so why not just ignore the first value? –  Evan Mulawski Aug 11 '11 at 15:11
Not the answer that I'm looking for, but it appears that this cannot be achieved using regular expressions...:/ –  mre Aug 11 '11 at 23:06

Your delimiter could be "either a whitespace sequence OR chunks of non-ws at the beginning of a string, but this leaves you an empty string at the front:

Arrays.toString("abc def  ghi   jkl".split("\\s+|^\\S+\\s+"))



That is the problem with split -- you will, I think, always get something at the beginning of your array. Unfortunately I think you need to whack off the front of the string before splitting, or use Java's Arrays.copyOfRange() or similar to post-process.

Dropping the beginning can be done with replaceFirst:

import java.util.Arrays;
public class SplitExample {

    public static final String data = "abc   def  ghi";
    public static void main(String[] args) {
        System.out.println(Arrays.toString(data.replaceFirst("^\\S+\\s+", "").split("\\s+")));

The final line is as close as I can get, because split produces matches AROUND your delimiters. How can you avoid the blank string at the front with a single split? I am not sure there is a way....

share|improve this answer

Since the consensus seems to be that it is impossible, I propose this solution:

Assuming you only have ONE space in the 'junk' value,

int ix = theString.indexOf(" ");
ix = theString.indexOf(" ", ix);
theString.substring(ix + 1).split("\\s");

This gets the substring from the second space in the string (the first space after the space in the 'junk' value) then splits it.

share|improve this answer

Would you be able to do this?

String[] array = foo.split("\\S*\\s")[1].split("\\s");

It's 2 regex splits instead of one, but it's neater than looping later. I'm not sure it's correct, but it should separate the string first into "any number of non-whitespace characters followed by a whitespace" and everything else. You can then split everything else by whitespace only, and you'll be left with an array excluding the first element.

Edit: Yeah, it can't be done with a single split since the only way to have anything other than "" as the first element in your array is to have something you're not removing with the split at the front of the string.

share|improve this answer

Your Answer


