简体   繁体   中英

Java RegEx: Split without losing token

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.

You are capturing the minus sign which is used to split. Therefore it is removed.

Two possible solutions:

a) add it to the second match because it has to be there otherwise the split wouldn't return this result b) try (\\S*) (\\S.*) instead and do a match. This will return two results, "x" and "-y".

If the split function is such a simple one consider using the string split function. Its much faster than regex.

var result = "x -y".split(" -");
if (result.length == 2) result[1] = "-" + result[1];

http://gskinner.com/RegExr/ is a nice site to check your regular expressions. If you compare your regex with Howards you will see the difference. If you take mine and do a match, too.

Pattern:

\s(?=\-\S)

Example"

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

Carets point to matches

Demo:

Example Code

If there will always be a space between the values you want to split, consider using something like:

"x -y".split(" -");

Just remember to put the - back in (if that is required).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM