i am first to this site. I want to split a string without the characters that matched in regex of string split method in java.
The string for splitting is (for eg.) : "conditional&&operator and ampersand&Symbol."
My regex-expression for spit is : "[^\\&]\\&[^\\&]"
My expectation is : [conditional&&operator and ampersand, Symbol]
But, the output is : [conditional&&operator and *ampersan, ymbol*]
The code i used is:
String s = "conditional&&operator and ampersand&Symbol.";
String[] sarr = s.split("[^\\&]\\&[^\\&]");
System.out.println(Arrays.toString(sarr));
So, please tell me what regex i should use to get the expected output, that is without the additional characters removed.
Your question is very similar to this one.
What you need is a negative look-behind. In your case, you could use something like:
String s = "conditional&&operator and ampersand&Symbol.";
String[] sarr = s.split("(?<!&)&(?!&)");
System.out.println(Arrays.toString(sarr));
// output: [conditional&&operator and ampersand, Symbol.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With