java - Split a string with multiple delimiters while keeping these delimiters -
let's have string:
string x = "a| b |c& d^ e|f";
what want obtain looks this:
a | b | c & d ^ e | f
how can achieve using x.split(regex)
? namely regex?
i tried link: how split string, keep delimiters?
it gives explanation on how 1 delimiter. however, using , modifying fit multiple delimiters (lookahead , lookbehind mechanism) not obvious not familiar regex.
the regex splitsplitting on optional spaces after word boundary
\\b\\s*
note \\b
checks if preceding character letter, or digit or underscore, , matches number of whitespace characters string split on.
here sample java code on ideone:
string str = "a| b |c& d^ e|f"; string regex = "\\b\\s*"; string[] spts = str.split(regex); for(int =0; < spts.length && < 20; i++) { system.out.println(spts[i]); }
Comments
Post a Comment