split a polynomial string with more the one delimiter and keep the delimiters? (JAVA) -


for example if got string "1+3-2+45-6" need array of strings {1,+3,-2,+45,-6}

i tried split twice, 1 time "+" , time "-". inconvenient. there easy way in java it?

thanks in advance!

use positive lookahead based regex. (?=[+-]) regex matches boundary exists before + or - symbols. splitting input according matched boundary give desired output.

string.split("(?=[+-])"); 

example:

string s = "1+3-2+45-6"; string[] parts = s.split("(?=[+-])"); system.out.println(arrays.tostring(parts)); 

output:

[1, +3, -2, +45, -6] 

Comments

Popular posts from this blog

python - Installing PyDev in eclipse is failed -

PHP OOP-based login system -

c# - Nested Internal Class with Readonly Hashtable throws Null ref exception.. on assignment -