java regex split string by multiple chars and spaces - java

I have a string, i need to split by chars and spaces. it can be 1 or more of chars/spaces..can be any number of chars.
String a="1a2bc3 4d5 6ads";
s.split(" ");
I want 1 2 3 4 5 6.
do advise how incorporated the characters

Here is a streamlined regex splitting solution:
String input = "1a2bc3 4d5 6ads";
String[] nums = input.split("\\D+");
System.out.println(Arrays.toString(nums)); // [1, 2, 3, 4, 5, 6]
The idea here is to split the input one groups of one or more non digit characters. This turns out an array consisting only of numbers, which were spared during the split.

Related

Single Digits From String In Java Regex

Given a string
1 3 2 1 9 1 bla 3 4 3
I found that
/b[1-4]/b
will return only the digits 1 2 3 4 as this shows but String[] input = args[0].split("\b[1-4]\b"); does not return
{"1","3","2","1","1","3","4","3"}
I assume that you only want digits between 1 and 4. A simple split is not going to be enough. One approach could be something like this:
String str = "1 3 2 1 9 1 bla 3 4 3";
String[] splitAndFilter = Pattern.compile("\\s+")
.splitAsStream(str)
.filter(s -> s.matches("[1-4]"))
.toArray(String[]::new);
System.out.println(Arrays.toString(splitAndFilter));
The problem with your current approach is that you are trying to split on the numbers themselves. This won't give the intended result, because that on which you split gets consumed (read: removed), leaving behind everything else. Instead, try splitting on [^1-4]+:
String input = "1 3 2 1 9 1 bla 3 4 3";
String[] parts = input.split("[^1-4]+");
System.out.println(Arrays.toString(parts));
This prints:
[1, 3, 2, 1, 1, 3, 4, 3]
This will split on one or more non 1-4 characters. This happens to work for your input string, because whitespace is a delimiter, and also the non matching digits and words should be removed.
You can use just [1-4] as the regex.
import java.util.Arrays;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
String[] matches = Pattern.compile("[1-4]")
.matcher(args[0])
.results()
.map(MatchResult::group)
.toArray(String[]::new);
System.out.println(Arrays.toString(matches));
}
}
Output:
[1, 3, 2, 1, 1, 3, 4, 3]
where the command-line argument is "1 3 2 1 9 1 bla 3 4 3"

Split comma-separated string but ignore comma followed by a space

public static void main(String[] args) {
String title = "Today, and tomorrow,2,1,2,5,0";
String[] titleSep = title.split(",");
System.out.println(Arrays.toString(titleSep));
System.out.println(titleSep[0]);
System.out.println(titleSep[1]);
}
output:
[Today, and tomorrow, 2, 1, 2, 5, 0]
Today
(space) and tomorrow
I want to treat "Today, and tomorrow" as a phrase representing the first index value of titleSep (do not want to separate at comma it contains).
What is the split method argument that would split the string only at commas NOT followed by a space?
(Java 8)
Use a negative look ahead:
String[] titleSep = title.split(",(?! )");
The regex (?! ) means "the input following the current position is not a space".
FYI a negative look ahead has the form (?!<some regex>) and a positive look ahead has the form (?=<some regex>)
The argument to the split function is a regex, so we can use a negative lookahead to split by comma-not-followed-by-space:
String title = "Today, and tomorrow,2,1,2,5,0";
String[] titleSep = title.split(",(?! )"); // comma not followed by space
System.out.println(Arrays.toString(titleSep));
System.out.println(titleSep[0]);
System.out.println(titleSep[1]);
The output is:
[Today, and tomorrow, 2, 1, 2, 5, 0]
Today, and tomorrow
2

Strip all whitespaces in string and convert it to an array in Java [duplicate]

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(" ");

Java String.split() - splitting a string using //s+ doesn't capture parentheses as separate elements?

Let's say I have a string: "(2 * 32) + 5 ^ 2"
I'd like to turn this into a String array: [(2, *, 32, ), +, 5, ^, 2]
i.e. I don't want to capture spaces in the original string and I want to split by whitespace characters.
So I tried string.split**("\\s+")** but the result looks like [(2,*,32), +, 5, ^, 2].
Can someone explain why it doesn't split "(2" into (,2? Thank you!
This works, and has the added benefit of not splitting when there are numbers longer than 1 digit, and not requiring spaces between tokens.
String str = "(2*32) + 5 ^ 2";
String[] tokens = str.replace(" ", "").split("\\b|(?=\\D)");
Output:
[ (, 2, *, 32, ), +, 5, ^, 2 ]
Ideone Demo

Splitting String in Java with empty elements

I'm reading from a .csv File line by line. One line could look for example as following: String str = "10,1,,,,".
Now I would like to split according to ",": String[] splitted = str.split(","); The problem now is that this only results in 2 elements but I would like to have 5 elements, the first two elements should contain 10 and 1 and the other 3 should be just an empty String.
Another example is String str = "0,,,,," which results in only one element but I would like to have 5 elements.
The last example is String str = "9,,,1,," which gives 2 elements (9 and 1), but I would like to have 5 elements. The first element should be 9 and the fourth element should be 1 and all other should be an empty String.
How can this be done?
You need to use it with -1 parameter
String[] splitted = str.split(",", -1);
This has been discussed before, e.g.
Java: String split(): I want it to include the empty strings at the end
But split really shouldn't be the way you parse a csv, you could run into problems when you have a String value containing a comma
23,"test,test","123.88"
split would split the row into 4 parts:
[23, "test, test", "123.88"]
and I don't think you want that.
split only drops trailing delimeters by default. You can turn this off with
String str = "9,,,1,,";
String[] parts = str.split(",", -1);
System.out.println(Arrays.toString(parts));
prints
[9, , , 1, , ]
Pass -1 (or any negative number, actually) as a second parameter to split:
System.out.println("0,,,,,".split(",", -1).length); // Prints 6.

Categories

Resources