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.
EDIT:
I'm looking to accomplish th开发者_Python百科is purely through regular expressions. If this is not possible, please let me know!
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
}
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+"))
produces
[,def,ghi]
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.split("\\s+")));
System.out.println(Arrays.toString(data.split("\\s+|^\\S+\\s+")));
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....
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.
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.
精彩评论