I have been trying to implement the seperation of string into string and number in java. As of now following pattern gives me number after string as follows:
String[] part = string.split("(?<=\\D)(?=\\d)");
Ans: "ABC123" -> ["ABC","123"]
But it fails in following scenario
"A1BC123" ->
expected output: ["A1BC","123"]
Kindly help
CodePudding user response:
You just need two alternate for non-digit and digit. Also you need to group them separately\
((?<=\\d)|(?=\\d))((?<=\\D)|(?=\\D))
This will separate all non-digit and digit like you said in your first paragraph, so assuming that what you want this will be the output.
"A1BC123" => ["A", "1", "BC", "123"]