Java regex: split without losing tokens

I'm trying to write a regular expression that splits a string when there is a space followed by a minus sign followed by a non space

Example:

"x -y".split(regex)
returns: String[]{"x","-y"};

I am currently using

(?<=\\s)-(?=\\S+)

For my regular expression; But this will return "X", "Y" and eat the minus sign Is there any way not to eat minus?

thank you!

Solution

Pattern:

\s(?=\-\S)

Example "

String:  x -y z -x y z
Matches:   ^    ^

Carets point to match

demonstration:

Example Code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>