Bhavin S. :
First time I use Regex statement.
I have java regex statement, which split String by pattern with list of some characters.
String line = "F01T8B02S00003H04Z05C0.12500";
Pattern pattern = Pattern.compile("([BCFHSTZ])");
String[] commands = pattern.split(line);
for (String command : commands) {
System.out.print(command);
}
output of above code is like (018020000304050.12500)
Actually I want output like this, ("F", "01", "T", "8", "B", "02", "S", "00003", "H", "04", "Z", "05", "C", "0.12500").
Means desired output is contains pattern character and split value both.
Can you please suggest me?
Kevin Cruijssen :
You can use a String#split on [A-Z]
which keeps the delimiter as separated item:
String line = "F01T8B02S00003H04Z05C0.12500";
String[] result = line.split("((?<=[A-Z])|(?=[A-Z]))");
System.out.println(java.util.Arrays.toString(result));
Which will result in the String-array:
[F, 01, T, 8, B, 02, S, 00003, H, 04, Z, 05, C, 0.12500]