So I'm trying to split a file name to the base and it's extension. I found this answer to this question, but I have another condition that this answer doesn't answer. I have a two conditions that I need to add:
1. If a file starts with "." and doesn't have any other period, then it counts like the first period is a part of the file's name, and there is no extension (the extension is ""). the answer in the link above
fileName.split("\\.(?=[^\\.]+$)")
will return for the given file name ".Myfile"
["",MyFile"]
but what I want it to return is
[.MyFile,""]
is there any way to do so using regex (changing the condition)?
2. I need 2 cells, no matter what the file name is. If the file name is "README " then I still want two cells to be created, the second one should contain an empty char.
I want:
[README,""]
to be returned.
Is this possible?
edit: solved! thanks to the help of Wiktor Stribiżew I solved it. I changed it to
String fileName;
String[] splitedName
String pat = "((?!^)\\.(?=[^.]*$|(?<=^\\.[^.]{0,1000})$))|$";
fileName = "README.txt"
System.out.println(Arrays.toString(fileName .split(pat,2)));
fileName = ".README"
System.out.println(Arrays.toString(fileName .split(pat,2)));
fileName = "README"
Result
"README.txt" => [REAADME,txt]
".README" => [REAADME, ]
"README" => [REAADME, ]
as I wanted.