Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java split by alphabeta char creates an empty value in array

I want to split my string on every occurrence of an alpha-beta character.

for example:

"s1l1e13" to an array of: ["s1","l1","e13"]

when trying to use this simple split by regex i get some weird results:

testStr = "s1l1e13"
Arrays.toString(testStr.split("(?=[a-z])"))

gives me the array of:

["","s1","l1","e13"]

how can i create the split without the empty array element?

I tried a couple more things:

testStr = "s1"
Arrays.toString(testStr.split("(?=[a-z])")) 

does return the currect array: ["s1"]

but when trying to use substring

testStr = "s1l1e13"
Arrays.toString(testStr.substring(1).split("(?=[a-z])")

i get in return ["1","l1","e13"]

what am i missing?

like image 648
amitben Avatar asked Jun 10 '26 10:06

amitben


1 Answers

Your Lookahead marks each position before any character of a to z; marking the following positions:

 s1 l1 e13
^  ^  ^

So by spliting using just the Lookahead, it returns ["", "s1", "l1", "e13"]

You can use a Negative Lookbehind here. This looks behind to see if there is not the beginning of the string.

String s = "s1l1e13";
String[] parts = s.split("(?<!\\A)(?=[a-z])");
System.out.println(Arrays.toString(parts)); //=> [s1, l1, e13]
like image 98
hwnd Avatar answered Jun 12 '26 12:06

hwnd