I need to split strings into two with a given separator using regex. Sample Strings and expected output as below
testnames ===> testnames,3 3alpha ====> 3alpha, 0 betavalue ===> betavalue, 2 gama4 ===> gama4, 23 tama ====> tama, 2334 tes[t[ ===> No matches tryt ===> No matches
Each and every String should be split into two if it has numbers within the square bracket at the end of the input string.
Can anyone tell me a regex to do this?
I find out the regex :
But this only gave me numbers inside the square bracket and not gives the rest of the string
You may use this greedy match replacement with 2 capture groups:
String repl = str.replaceFirst("^(.*)\[(\d+)\]", "$1, $2");
(.*): Greedily match 0 or more of any characters in capture group #1
[: Match a
(d+): Match 1 or more of digits in capture group #2
]: Match a