I am trying to split an input string that contains whitespace, but I do not want to cut it off from my split, I want to include it in my split array. Is there a better regex or method to use in this case?
String data = "1 a1 b1 r5";
String splitData = data.split("\\s+");
for(String x : splitData){
System.out.print(x + ", ");
}
Expected output: 1, , , , , ,a1, b1, , r5
I'm confused by your methodology here. If this is all you're trying to do, it can be done much more simply:
String input = "1 a1 b1 r5";
String output = input.replace(" ", ", ");
System.out.println(output);
The middle line simply replaces the space character, " ", with a comma followed by a space, ", ". The final output matches your requested output:
1, , , , , a1, b1, , r5
If this is a minimal example and you actually intend to use a more complex regex, please post that regex and we can get to work on it.
If you want to create an array of tokens, just use:
String[] sp = s.split( " " );
However, this will create 4 empty items after the first one, not 5.
There is 1 space between "a1" and "b1", and you expect 0 empty items there.
There are 2 spaces between "b1" and "r5" and you expect 1 empty item there.
There are 5 empty spaces between "1" and "a1". Why do you expect 5 empty items there instead of 4?
And why does your expected output not have a space after the comma in front of "a1" ?
Related
This question already has answers here:
How to split a string with any whitespace chars as delimiters
(13 answers)
Closed 5 years ago.
I'm looking for a way to convert a string to an array and strip all whitespaces in the process. Here's what I have:
String[] splitArray = input.split(" ").trim();
But I can't figure out how to get rid of spaces in between the elements.
For example,
input = " 1 2 3 4 5 "
I want splitArray to be:
[1,2,3,4,5]
First off, this input.split(" ").trim(); won't compile since you can't call trim() on an array, but fortunately you don't need to. Your problem is that your regex, " " is treating each space as a split target, and with an input String like so:
String input = " 1 2 3 4 5 ";
You end up creating an array filled with several empty "" String items.
So this code:
String input = " 1 2 3 4 5 ";
// String[] splitArray = input.split("\\s+").trim();
String[] splitArray = input.trim().split(" ");
System.out.println(Arrays.toString(splitArray));
will result in this output:
[1, , , , , , , , 2, 3, 4, , , , , , 5]
What you need to do is to create a regex that greedily groups all the spaces or whitespace characters together, and fortunately we have this ability -- the + operator
Simply use a greedy split with the whitespace regex group
String[] splitArray = input.trim().split("\\s+");
\\s denotes any white-space character, and the trailing + will greedily aggregate one or more contiguous white-space characters together.
And actually, in your situation where the whitespace is nothing but multiples of spaces: " ", this is adequate:
String[] splitArray = input.trim().split(" +");
Appropriate tutorials for this:
short-hand character classes -- discusses \\s
repetition -- discusses the + also ? and * repetition characters
Try:
String[] result = input.split(" ");
I'm trying to split some input from BufferedReader.readLine()
String delimiters = " ,()";
String[] s = in.readLine().split(delimiters);
This gives me a runtime error.
Things I have tried that don't work:
String delimiters = " ,\\(\\)";
String delimiters = " ,[()]";
String[] s = in.readLine().split(Pattern.quote("() ,"));
I tried replacing the () using .replaceAll, didn't work
I tried this:
input = input.replaceAll(Pattern.quote("("), " ");
input = input.replaceAll(Pattern.quote(")"), " ");
input = input.replaceAll(Pattern.quote(","), " ");
String[] s = input.split(" ");
but s[] ends up with blank slots that look like this -> "" no clue why its doing that
Mine works, for
String delimiters = "[ \\(\\)]"
Edit:
You forgot Square brakcets which represents, "Any of the characters in the box will be used as delimiters", its a regex.
Edit:
To remove the empty elements: Idea is to replace any anagram of set of delimiters to just 1 delimiter
Like.
// regex to match any anagram of a given set of delimiters in square brackets
String r = "(?!.*(.).*\1)[ \\(\\)]";
input = input.replaceAll(r, "(");
// this will result in having double or more combinations of a single delimiter, so replace them with just one
input = input.replaceAll("[(]+", "(");
Then you will have the input, with any single delimiter. Then use the split, it will not have any blank words.
From your comment:
but I am only input 1 line: (1,3), (6,5), (2,3), (9,1) and I need 13652391 so s[0] = 1, s[1]=3, ... but I get s[0] = "" s[1] = "" s[2] = 1
You get that because your delimiters are either " ", ",", "(" or ")" so it will split at every single delimiter, even if there is no other characters between them, in which case it will be split into an empty string.
There is an easy fix to this problem, just remove the empty elements!
List<String> list = Arrays.stream(
"(1,3), (6,5), (2,3), (9,1)".split("[(), ]")).filter(x -> !x.isEmpty())
.collect(Collectors.toList());
But then you get a List as the result instead of an array.
Another way to do this, is to replace "[(), ]" with "":
String result = "(1,3), (6,5), (2,3), (9,1)".replaceAll("[(), ]", "");
This will give you a string as a result. But from the comment I'm not sure whether you wanted a string or not. If you want an array, just call .split("") and it will be split into individual characters.
Have a look at this string String str = "first,second, there"; . there are , and , ( there's a space after the second comma). How do I express it in RegEx as .replaceAll()'s parameters so the
output would be :
"first second there". <-- the amount on each space will be same.
I had tried some combinations but still fail. One of them is :
String temp2 = str.replaceAll("[\\,\\, ]", " "); will print first second there.
Thanks before.
Simply use , * to match comma followed by zero or more spaces and replace with single space.
String str = "first,second, there";
System.out.println(str.replaceAll(", *", " "));
output:
first second there
Read more about Java Pattern
Greedy quantifiers
X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
String temp2 = str.replaceAll(", ?", " ");
The ? Means optional (ie zero or once), or
String temp2 = str.replaceAll(", *", " ");
Where the * means zero or more (many) spaces
I have strings in the following form:
let the character - denote empty space
----100----100----1000---
that is, more empty spaces followed by a number, followed by more empty spaces followed by number, etc.
I need to extract the three numbers only. How do I do that in java?
Thanks
I need to extract the three numbers only. How do I do that in java?
So I understand your string is like below, in which case you can split it on white spaces:
String str = " 100 100 1000";
String[] numbers = str.trim().split("\\s+");
To collapse the spaces (the question asked):
String collapsed = str.replaceAll(" +", " ");
To extract the 3 numbers (the question alluded to):
String[] numbers = str.trim().split(" +");
simply try using,
String newstring = oldstring.replaceAll(" +", " ");
or
String[] selected = oldstring.trim().split(" +");
I am trying to split a string in Java on / but I need to ignore any instances where / is found between []. For example if I have the following string
/foo/bar[donkey=King/Kong]/value
Then I would like to return the following in my output
foo
bar[donkey=King/Kong]
value
I have seen a couple other similar posts, but I haven't found anything that fits exactly what I'm trying to do. I've tried the String.split() method and as follows and have seen weird results:
Code: value.split("/[^/*\\[.*/.*\\]]")
Result: [, oo, ar[donkey=King, ong], alue]
What do I need to do in order to get back the following:
Desired Result: [, foo, bar[donkey=King/Kong], value]
Thanks,
Jeremy
You need to split on the / followed by an 0 or more balanced pairs of brackets:
String str = "/foo/bar[donkey=King/Kong]/value";
String[] arr = str.split("/(?=([[^\\[\\]]*\\[[^\\[\\]]*\\])*[^\\[\\]]*$)");
System.out.println(Arrays.toString(arr));
Output:
[, foo, bar[donkey=King/Kong], value]
More User friendly explanation
String[] arr = str.split("(?x)/" + // Split on `/`
"(?=" + // Followed by
" (" + // Start a capture group
" [^\\[\\]]*" + // 0 or more non-[, ] character
" \\[" + // then a `[`
" [^\\]\\[]*" + // 0 or more non-[, ] character
" \\]" + // then a `]`
" )*" + // 0 or more repetition of previous pattern
" [^\\[\\]]*" + // 0 or more non-[, ] characters
"$)"); // till the end
Of the following string, the regex below will match foo and bar, but not fox and baz, because they're followed by a close bracket. Study up on negative lookahead.
fox]foo/bar/baz]
Regex:
\b(\w+)\b(?!])