I'm trying to write regex that will split a string when there is whitespace followed by a negative sign followed by non whitespace.
Example:
"x -y".split(regex)
returns: String[]{"x","-y"};
Currently I'm using
(?<=\\s)-(?=\\S+)
for my regex; but this returns "x","y" and eats the negative sign. is there any way to not eat the negative sign?
Thanks!
You may include the minus in the second group
\\s(?=-\\S+)
This gives you the desired result.
Pattern:
\s(?=\-\S)
Example"
String: x -y z -x y z
Matches: ^ ^
Carets point to matches
Demo:
Example Code
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