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

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -