I'm currently trying to split a string by whitespaces in Java, except everything between the first and last double quotes (this is a specific case and they are guaranteed to exist). I'm fairly new to regex but here's what I came up with to find the substring within the first and last quotes:
regex = ((?<=\").*(?=\"))
I have tested it out on https://regex101.com and am satisfied with the result. But now when I try to split my input string like the following:
String input = "apple \"banana\"";
String[] arr = input.split("\\s+((?<=\").*(?=\"))");
It fails to do the actual splitting. Here is the expected output:
arr[0] == "apple", arr[1] == "banana"
The output I recieve:
arr[0] == "apple \"banana\""
What am I missing here? Any guidance would be greatly appreciated.
EDIT: Answer has been given by @WiktorStribiżew in the comments.
A big thank you to everybody who helped, especially @WiktorStribiżew Cheers!