Home > Software engineering >  Get number only after last string using regex
Get number only after last string using regex

Time:07-28

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"]
  •  Tags:  
  • java
  • Related